chat.rkt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #lang typed/racket
  2. (provide (all-defined-out))
  3. (require/typed "unheck-html.rkt"
  4. [unheck-html (-> String String)]
  5. [unheck-all-html (-> String String)])
  6. (require typed/net/url
  7. "private/ws-typed.rkt" ;; web sockets
  8. typed/json
  9. "api.rkt")
  10. ;; don't let the websockets timeout by themselves
  11. (ws-idle-timeout +inf.0)
  12. ;; Creates a new connection and spaws threads for handling incoming events
  13. ;; and for sending pings.
  14. ;; Returns an object that can be used with e.g. send-message
  15. (: make-connection
  16. (->* (String ;; Server
  17. String ;; User name
  18. #:on-chat (-> String String Void)
  19. #:on-response (-> String Void)
  20. #:on-users (-> (Listof String) Void)
  21. #:on-notify (-> String Void)
  22. #:on-join (-> String Void)
  23. #:on-leave (-> String Void)
  24. #:on-name-change (-> String String Void)
  25. #:on-close-conn (-> Void)
  26. #:on-topic (-> String Void)) ()
  27. WS))
  28. (define (make-connection server-addr user-name
  29. #:on-chat on-chat ;;; called with (on-chat from message)
  30. #:on-response on-response ;;; called with (on-response message)
  31. #:on-users on-users ;;; called with (on-users users)
  32. #:on-notify on-notify ;;; called with (on-notify msg)
  33. #:on-join on-join ;;; called with (on-join user)
  34. #:on-leave on-leave ;;; called with (on-leave user)
  35. #:on-name-change on-name-change
  36. ;;; called with (on-name-change old-name new-name)
  37. #:on-close-conn on-close-conn ;;; called with (on-close-conn)
  38. #:on-topic on-topic ;;; called with (on-topic topic)
  39. )
  40. (define c (ws-connect (string->url server-addr)))
  41. (define user-color "#00FFAA")
  42. (define evt (ws-recv-evt c))
  43. (: handle-evt (-> Void))
  44. (define (handle-evt)
  45. (let ([v (sync evt)])
  46. (cond
  47. [(eof-object? v)
  48. (log-warning "RIP websocket\n")
  49. (ws-close! c)
  50. (on-close-conn)]
  51. [(string? v)
  52. (let ([js (string->jsexpr v)])
  53. (cond
  54. [(get-chat-message js)
  55. => (lambda (msg)
  56. (match msg
  57. [(chat-message message from color type)
  58. (match type
  59. ['chat (on-chat from (unheck-html message))]
  60. ['response (on-response (unheck-all-html message))]
  61. [_
  62. (log-warning
  63. (format "chat.rkt/handl-evt: cannot handle message type in ~s" msg))])]))]
  64. [(get-users-reply js) => on-users]
  65. [(get-notify js) => on-notify]
  66. [(get-topic js) => on-topic]
  67. [(get-event-data js)
  68. => (lambda (ed)
  69. (match ed
  70. [(event-data type (? string? data))
  71. (match type
  72. ['join (on-join data)]
  73. ['leave (on-leave data)]
  74. [(or 'name-changed
  75. 'name-change-forced)
  76. (match (string-split data ":")
  77. [(list old-nick new-nick)
  78. (on-name-change old-nick new-nick)]
  79. [_ #f])]
  80. [_
  81. (log-warning
  82. (format "chat.rkt/handl-evt: cannot handle event type in ~s" ed))])]))]
  83. [else
  84. (log-warning
  85. (format "chat.rkt/handle-evt: don't know how to handle ~a" (jsexpr->string js)))]))
  86. (handle-evt)]
  87. [else
  88. (printf "Unknown msg: ~a" v)
  89. (handle-evt)])))
  90. (: do-ping (-> Void))
  91. (define (do-ping)
  92. (sleep 10)
  93. (unless (ws-conn-closed? c)
  94. (send-ping c)
  95. (do-ping)))
  96. (void (thread handle-evt))
  97. (void (thread do-ping))
  98. c)