Posted to tcl by mbedev at Fri Mar 01 12:54:47 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"

        # 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 } {
        fconfigure $chan -blocking false
        #puts [::websocket::conninfo $chan state]
        if { [::websocket::test $::sock $chan /] } {
                ::websocket::upgrade $chan
        } else {
                puts stderr "Not a websocket"
                return
        }
}

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

#if 0 {
        ::websocket::server $sock
        #puts stderr [::websocket::conninfo $sock state]
        ::websocket::live $sock / simple_handler
        #puts stderr [::websocket::conninfo $sock state]
#}

vwait forever