Posted to tcl by mbedev at Fri Mar 01 12:54:47 GMT 2019view raw

  1. package require websocket
  2. ::websocket::loglevel debug
  3.  
  4. proc simple_handler {sock type msg} {
  5. # Uncomment the following line to view what's being sent from the client.
  6. puts "Echo sock=$sock type=$type msg=$msg"
  7.  
  8. # All we want to do is echo back what was sent (thought I would append
  9. # my own message ;-))
  10.  
  11. switch $type {
  12. request {return }
  13. close { return }
  14. disconnect { return }
  15. binary { return }
  16. text {
  17. ::websocket::send $sock text "$msg on the TclHttpd Web Server"
  18. }
  19. }
  20. }
  21.  
  22. proc init_handler { chan clientaddr clientport } {
  23. fconfigure $chan -blocking false
  24. #puts [::websocket::conninfo $chan state]
  25. if { [::websocket::test $::sock $chan /] } {
  26. ::websocket::upgrade $chan
  27. } else {
  28. puts stderr "Not a websocket"
  29. return
  30. }
  31. }
  32.  
  33. set sock [socket -server init_handler 8080]
  34. fconfigure $sock -translation binary
  35.  
  36. #if 0 {
  37. ::websocket::server $sock
  38. #puts stderr [::websocket::conninfo $sock state]
  39. ::websocket::live $sock / simple_handler
  40. #puts stderr [::websocket::conninfo $sock state]
  41. #}
  42.  
  43. vwait forever
  44.