-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
executable file
·60 lines (45 loc) · 1.37 KB
/
server.rb
File metadata and controls
executable file
·60 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env ruby
lib_path = File.expand_path("./lib")
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
require 'CLIENT_HANDLER'
require 'http_server'
require 'eventmachine'
require 'em-websocket'
require 'json'
SERVER_IP = '0.0.0.0'
WEBSOCKET_SERVER_PORT = '8081'
HTTP_SERVER_PORT = '8080'
puts "SocketGame WS Server starting at ws://#{SERVER_IP}:#{WEBSOCKET_SERVER_PORT}"
puts "SocketGame HTTP Server starting at http://#{SERVER_IP}:#{HTTP_SERVER_PORT}"
class GameState
@@state = {}
def self.state
@@state
end
def self.state= (_state)
@@state = _state
end
end
CLIENT_HANDLER = SocketGame::ClientHandler.new
EventMachine.run do
# hit Control + C to stop
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
SocketGame::HttpServer.run!({ :bind => '0.0.0.0', :port => HTTP_SERVER_PORT})
EventMachine::WebSocket.start(:host => SERVER_IP, :port => WEBSOCKET_SERVER_PORT, :debug => false) do |socket|
socket.onopen do |data|
puts "onopen called..."
end
socket.onmessage do |data|
begin
message = JSON.parse(data)
CLIENT_HANDLER.message_recieved(message, socket)
rescue JSON::ParserError
socket.send( {error: "server only communicates in JSON"}.to_json )
end
end
socket.onclose do
CLIENT_HANDLER.delete_client(socket)
end
end
end