-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
36 lines (29 loc) · 815 Bytes
/
server.rb
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
require 'em-websocket'
@sockets = {}
#TODO: Move environmental config stuff to config
require './lib/database/development'
require './lib/app/controller'
@controller = Controller.new
EventMachine.run do
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen do
puts "WebSocket connection open"
@sockets[ws] = "Anon"
@sockets.each { |s,v| s.send "Anon has joined" }
end
ws.onclose do
puts "Connection closed"
broadcast "#{@sockets[ws]} has quit"
@sockets.delete(ws)
end
ws.onmessage do |msg|
puts "Recieved message: #{msg}"
begin
ws.send @controller.parse(msg)
rescue Exception => e
puts e.message
ws.send("Some kind of error happened")
end
end
end
end