Posted to tcl by mbedev at Fri Mar 01 13:28:19 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. if {0} {
  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.  
  24. proc init_handler { chan clientaddr clientport } {
  25. puts "server: new connection $clientaddr $clientport"
  26. ::websocket::takeover $chan simple_handler 1
  27. }
  28.  
  29. set sock [socket -server init_handler 8080]
  30. fconfigure $sock -translation binary
  31.  
  32. vwait forever
  33.  
  34.  
  35. #------- Output ---------
  36.  
  37. server: new connection 127.0.0.1 38629
  38. Echo sock=sock55c05cfecd50 type=connect msg=
  39. [Fri Mar 01 14:25:56 CET 2019] [websocket] [debug] 'sock55c05cfecd50 has been registered as a server WebSocket'
  40. [Fri Mar 01 14:25:56 CET 2019] [websocket] [warn] 'Cannot send along WS sock55c05cfecd50, not connected'
  41. [Fri Mar 01 14:25:56 CET 2019] [websocket] [info] 'Closing web socket: 1002 (Protocol error)'
  42. Echo sock=sock55c05cfecd50 type=close msg=1002 {Protocol error}
  43. Echo sock=sock55c05cfecd50 type=disconnect msg=Disconnected from remote end
  44.