Posted to tcl by mbedev at Fri Mar 01 12:43:56 GMT 2019view raw

  1. package require websocket
  2. ::websocket::loglevel debug
  3.  
  4. # I stole this handler from the wiki
  5. proc simple_handler {sock type msg} {
  6. # Uncomment the following line to view what's being sent from the client.
  7. puts "Echo sock=$sock type=$type msg=$msg"
  8.  
  9. # All we want to do is echo back what was sent (thought I would append
  10. # my own message ;-))
  11.  
  12. switch $type {
  13. request {return }
  14. close { return }
  15. disconnect { return }
  16. binary { return }
  17. text {
  18. ::websocket::send $sock text "$msg on the TclHttpd Web Server"
  19. }
  20. }
  21. }
  22.  
  23. proc init_handler { chan clientaddr clientport } {
  24. fconfigure $chan -blocking false
  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.