【问题标题】:How can I handle websocket disconnect (from page reload) in Aleph?如何在 Aleph 中处理 websocket 断开连接(从页面重新加载)?
【发布时间】:2013-08-31 05:39:30
【问题描述】:

我使用 aleph 编写了一个基于事件的 websocket 处理程序。核心文件如下所示:

(defn handle-message
  "Handle a message"
  [message]
  (event/dispatch message))

(defn websocket-handler
  "Handle websocket connections."
  [client-node connection-data]
  (map* #(handle-message (message/create client-node connection-data %)) client-node))

(defn -main
  "Start the http server and start handling websocket connections."
  [& args]
  (myapp.routes.api/add-events)
  (start-http-server websocket-handler {:port 8080 :websocket true}))

这很好用,我有一些可以正常工作并提供预期输出的事件。当我重新加载页面时,它停止工作。有什么问题?我假设发生了断开/重新连接。如何管理服务器端的断开连接?

【问题讨论】:

  • 什么是事件/调度?

标签: clojure websocket


【解决方案1】:

在你的客户端连接函数中,添加一个 fn 来处理当他们的通道结束时要做什么:

(defn connection-handler [channel request]
 (lamina/on-closed channel handle-client-disconnected-fn)
)

(defroutes my-routes
  (GET "/my-website-path" [] (wrap-aleph-handler connection-handler))
)

(start-http-server
    (->
      my-routes
      (wrap-session)
      (wrap-file "./public")
      (wrap-file-info)
      (wrap-stacktrace)
      (wrap-ring-handler)
    )
    {:port 8080 :websocket true}
)

编辑:在实践中,检查它们是否尚未连接,因此您不会多次添加处理程序

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2018-08-22
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多