Posted to tcl by mbedev at Fri Mar 01 13:28:19 GMT 2019view pretty

package require websocket
::websocket::loglevel debug

proc simple_handler {sock type msg} {
        # Uncomment the following line to view what's being sent from the client.
        puts "Echo sock=$sock type=$type msg=$msg"

        if {0} {
                # All we want to do is echo back what was sent (thought I would append
                # my own message ;-))

                switch $type {
                         request {return }
                         close { return }
                         disconnect { return }
                         binary { return }
                         text {
                                 ::websocket::send $sock text "$msg on the TclHttpd Web Server"
                         }
                }
        }
}

proc init_handler { chan clientaddr clientport } {
        puts "server: new connection $clientaddr $clientport"
        ::websocket::takeover $chan simple_handler 1
}

set sock [socket -server init_handler 8080]
fconfigure $sock -translation binary

vwait forever


#------- Output ---------

server: new connection 127.0.0.1 38629
Echo sock=sock55c05cfecd50 type=connect msg=
[Fri Mar 01 14:25:56 CET 2019] [websocket] [debug] 'sock55c05cfecd50 has been registered as a server WebSocket'
[Fri Mar 01 14:25:56 CET 2019] [websocket] [warn] 'Cannot send along WS sock55c05cfecd50, not connected'
[Fri Mar 01 14:25:56 CET 2019] [websocket] [info] 'Closing web socket: 1002 (Protocol error)'
Echo sock=sock55c05cfecd50 type=close msg=1002 {Protocol error}
Echo sock=sock55c05cfecd50 type=disconnect msg=Disconnected from remote end