Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dan Simpson
committed
Dec 15, 2009
1 parent
020b9c7
commit 9e5dbb0
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,124 @@ | |||
$:.unshift File.dirname(__FILE__) + '/../lib' | |||
|
|||
require 'rubygems' | |||
require 'em-websocket-server' | |||
require 'json' | |||
require 'uuid' | |||
|
|||
$games = {} | |||
$waiting = nil | |||
|
|||
class Game < EM::Channel | |||
|
|||
attr_accessor :id, :p1, :p2, :current | |||
|
|||
def initialize p1, p2 | |||
super() | |||
@id = UUID.new | |||
@p1 = p1 | |||
@p2 = p2 | |||
end | |||
|
|||
def set_turn p | |||
@current = p | |||
@current.turn! | |||
end | |||
|
|||
def start! | |||
@current = nil | |||
toggle | |||
end | |||
|
|||
def move p, data | |||
if @current == p | |||
c = @p1 == p ? "x" : "o" | |||
@p1.send_move(c, data) | |||
@p2.send_move(c, data) | |||
toggle | |||
end | |||
end | |||
|
|||
def toggle | |||
set_turn(@current == @p1 ? @p2 : @p1) | |||
end | |||
end | |||
|
|||
class TickTackToeServer < WebSocketServer | |||
|
|||
attr_accessor :game_id | |||
|
|||
def on_connect | |||
end | |||
|
|||
def on_disconnect | |||
if $games.key?(game_id) | |||
$games.delete!(game_id) | |||
end | |||
end | |||
|
|||
def on_receive data | |||
|
|||
puts data | |||
|
|||
begin | |||
msg = JSON.parse(data) | |||
rescue | |||
send_command "error" | |||
return | |||
end | |||
|
|||
case msg["msg"] | |||
when "join" | |||
if $waiting.empty? | |||
$waiting.push(self) | |||
send_command "queued" | |||
else | |||
$waiting.pop do |opponent| | |||
game = Game.new(self, opponent) | |||
self.game_id = opponent.game_id = game.id | |||
game.start! | |||
end | |||
end | |||
when "move" | |||
if game | |||
game.move self, msg["data"] | |||
|
|||
else | |||
log "Cannot move on a nil game!" | |||
end | |||
end | |||
end | |||
|
|||
def game | |||
$games[game_id] | |||
end | |||
|
|||
def turn! | |||
send_command "turn" | |||
end | |||
|
|||
def game_over! | |||
send_command "game_over" | |||
end | |||
|
|||
def start! | |||
send_command "start" | |||
end | |||
|
|||
def send_move key, data | |||
|
|||
end | |||
|
|||
def send_command cmd | |||
send_message({:msg => cmd}.to_json) | |||
end | |||
end | |||
|
|||
|
|||
EM.epoll | |||
EM.set_descriptor_table_size(10240) | |||
|
|||
EM.run do | |||
$waiting = EM::Queue.new | |||
EM.start_server "0.0.0.0", 8000, TickTackToeServer | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
<!doctype html> | |||
<html> | |||
<head> | |||
<title>em-websocket-server test</title> | |||
</head> | |||
<body> | |||
|
|||
<h1>em-websocket-server timesync demo</h1> | |||
<h2 id="time">opening socket</h2> | |||
|
|||
<script> | |||
var webSocket = new WebSocket('ws://192.168.0.2:8000/time'); | |||
|
|||
webSocket.onopen = function(event){ | |||
document.getElementById('time').innerHTML = 'waiting for socket'; | |||
webSocket.send("{\"msg\":\"join\"}"); | |||
}; | |||
|
|||
webSocket.onmessage = function(event){ | |||
var object = JSON.parse(event.data); | |||
console.log(object); | |||
document.getElementById('time').innerHTML = object.time; | |||
}; | |||
|
|||
webSocket.onclose = function(event){ | |||
document.getElementById('time').innerHTML = 'socket closed'; | |||
}; | |||
</script> | |||
|
|||
</body> | |||
</html> |