2013-07-22 25 views
1

我已經使用aleph編寫了一個基於事件的websocket處理程序。核心文件是這樣的:如何處理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})) 

這工作得很好,我有工作,並給予預期輸出的一些事件。當我重新加載頁面時,它停止工作。有什麼問題?我假設有一個斷開/重新連接發生。我如何管理服務器端的斷開連接?

+0

什麼是事件/分派? – DanLebrero

回答

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

編輯:在實踐中,檢查它們是不是已經連接,所以你不要」不止一次添加處理程序

相關問題