diff --git a/src/cable/handler.cr b/src/cable/handler.cr index 5194dd7..e3b26d7 100644 --- a/src/cable/handler.cr +++ b/src/cable/handler.cr @@ -25,7 +25,7 @@ module Cable # Send welcome message to the client socket.send({type: Cable.message(:welcome)}.to_json) - Cable::WebsocketPinger.build(socket) + ws_pinger = Cable::WebsocketPinger.build(socket) socket.on_ping do socket.pong context.request.path @@ -44,6 +44,7 @@ module Cable end socket.on_close do + ws_pinger.stop Cable.server.remove_connection(connection_id) Cable::Logger.info { "Finished \"#{path}\" [WebSocket] for #{remote_address} at #{Time.utc.to_s}" } end diff --git a/src/cable/websocket_pinger.cr b/src/cable/websocket_pinger.cr index c607e03..168d856 100644 --- a/src/cable/websocket_pinger.cr +++ b/src/cable/websocket_pinger.cr @@ -5,6 +5,7 @@ module Cable class PingStoppedException < Exception; end @@seconds : Int32 | Float64 = 3 + @task : Tasker::Task def self.run_every(value : Int32 | Float64, &block) @@seconds = value @@ -23,10 +24,14 @@ module Cable end def initialize(@socket : HTTP::WebSocket) - Tasker.every(Cable::WebsocketPinger.seconds.seconds) do + @task = Tasker.every(Cable::WebsocketPinger.seconds.seconds) do raise PingStoppedException.new("Stopped") if @socket.closed? @socket.send({type: Cable.message(:ping), message: Time.utc.to_unix}.to_json) end end + + def stop + @task.cancel + end end end