chat.rkt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #:on-nick-collision (-> Void)) ()
  28. WS))
  29. (define (make-connection server-addr user-name
  30. #:on-chat on-chat ;;; called with (on-chat from message)
  31. #:on-response on-response ;;; called with (on-response message)
  32. #:on-users on-users ;;; called with (on-users users)
  33. #:on-notify on-notify ;;; called with (on-notify msg)
  34. #:on-join on-join ;;; called with (on-join user)
  35. #:on-leave on-leave ;;; called with (on-leave user)
  36. #:on-name-change on-name-change
  37. ;;; called with (on-name-change old-name new-name)
  38. #:on-close-conn on-close-conn ;;; called with (on-close-conn)
  39. #:on-topic on-topic ;;; called with (on-topic topic)
  40. #:on-nick-collision on-nick-collision
  41. )
  42. (define c (ws-connect (string->url server-addr)))
  43. (define user-color "#00FFAA")
  44. (define evt (ws-recv-evt c))
  45. (: handle-evt (-> Void))
  46. (define (handle-evt)
  47. (let ([v (sync evt)])
  48. (cond
  49. [(eof-object? v)
  50. (log-warning "RIP websocket\n")
  51. (ws-close! c)
  52. (on-close-conn)]
  53. [(string? v)
  54. (let ([js (string->jsexpr v)])
  55. (cond
  56. [(get-chat-message js)
  57. => (lambda (msg)
  58. (match msg
  59. [(chat-message message from color type)
  60. (match type
  61. ['chat (on-chat from (unheck-html message))]
  62. ['response (on-response (unheck-all-html message))]
  63. ['command-error (on-response (unheck-all-html message))]
  64. [_
  65. (log-warning
  66. (format "chat.rkt/handl-evt: cannot handle message type in ~s" msg))])]))]
  67. [(get-users-reply js) => on-users]
  68. [(get-notify js)
  69. => (lambda (note)
  70. (if (equal? note "Name already taken")
  71. (on-nick-collision)
  72. (on-notify note)))]
  73. [(get-topic js) => on-topic]
  74. [(get-event-data js)
  75. => (lambda (ed)
  76. (match ed
  77. [(event-data type (? string? data))
  78. (match type
  79. ['join (on-join data)]
  80. ['leave (on-leave data)]
  81. [(or 'name-changed
  82. 'name-change-forced)
  83. (match (string-split data ":")
  84. [(list old-nick new-nick)
  85. (on-name-change old-nick new-nick)]
  86. [_ #f])]
  87. [_
  88. (log-warning
  89. (format "chat.rkt/handl-evt: cannot handle event type in ~s" ed))])]))]
  90. [else
  91. (log-warning
  92. (format "chat.rkt/handle-evt: don't know how to handle ~a" (jsexpr->string js)))]))
  93. (handle-evt)]
  94. [else
  95. (printf "Unknown msg: ~a" v)
  96. (handle-evt)])))
  97. (: do-ping (-> Void))
  98. (define (do-ping)
  99. (sleep 10)
  100. (unless (ws-conn-closed? c)
  101. (send-ping c)
  102. (do-ping)))
  103. (void (thread handle-evt))
  104. (void (thread do-ping))
  105. c)