Posted to tcl by octetta at Sun Apr 12 17:27:02 GMT 2020view raw

  1. set http_ver [package require http]
  2. puts "http version $http_ver"
  3.  
  4. set ws_ver [package require websocket]
  5. puts "websocket version $ws_ver"
  6.  
  7. ::websocket::loglevel debug
  8.  
  9. proc handler {sock type msg} {
  10. puts "type -> $type"
  11. switch -- $type {
  12. connect - pong {
  13. puts "connect/pong"
  14. }
  15. disconnect {
  16. puts "disconnect"
  17. }
  18. text {
  19. puts "text -> $msg"
  20. }
  21. }
  22. }
  23.  
  24. #set url ws://echo.websocket.org/?encoding=text
  25. #set url ws://echo.websocket.org/
  26. # set url ws://echo.websocket.org
  27. set url ws://127.0.0.1:8080/ws
  28.  
  29. set ws [::websocket::open $url handler]
  30.  
  31. proc listen {when} {
  32. set ender 1
  33. after $when set ender 0
  34. vwait ender
  35. }
  36.  
  37. proc send {sock msg} {
  38. ::websocket::send $sock text $msg
  39. }
  40.  
  41. listen 400
  42.  
  43. send $ws "ponderosa"
  44. send $ws "funderosa"
  45. send $ws "sunderosa"