Permalink
Browse files

Better handling of connection closing and errors

  • Loading branch information...
1 parent b66c3a1 commit bfa598292737a06fa52f8317928ff86250e6b9a2 @dhotson committed Sep 10, 2012
Showing with 11 additions and 6 deletions.
  1. +11 −6 deathmatch.rb
View
@@ -409,11 +409,20 @@ def collide?(line)
player = $world.new_player
ws.onopen do
- EventMachine::PeriodicTimer.new(dt) do
+ t = EventMachine::PeriodicTimer.new(dt) do
ws.send $world.to_json(player)
end
- end
+ ws.onclose do
+ t.cancel
+ $world.remove_player(player)
+ end
+
+ ws.onerror do
+ t.cancel
+ $world.remove_player(player)
+ end
+ end
ws.onmessage do |msg|
if !player.dead
@@ -440,8 +449,4 @@ def collide?(line)
end
end
end
-
- ws.onclose do
- $world.remove_player(player)
- end
end

0 comments on commit bfa5982

Please sign in to comment.