1

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}))

これは正常に機能し、機能して期待される出力を提供するイベントがいくつかあります。ページをリロードすると、機能しなくなります。どうしたの?切断/再接続が発生していると思います。サーバー側で切断を管理するにはどうすればよいですか?

4

1 に答える 1

2

クライアントが接続するための関数で、チャネルの端が閉じたときに何をすべきかを処理する 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}
)

編集:実際には、ハンドラーを複数回追加しないように、それらがまだ接続されていないことを確認してください

于 2013-07-23T01:06:28.037 に答える