2012-07-23 22:22:23 +00:00
|
|
|
$LOAD_PATH << './lib'
|
|
|
|
require 'rubygems'
|
|
|
|
require 'eventmachine'
|
|
|
|
require 'em-websocket'
|
|
|
|
require 'json'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'web_socket'
|
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
# Look for -o with argument, and -I and -D boolean arguments
|
2012-08-06 06:29:39 +00:00
|
|
|
@options = {
|
2012-07-23 22:22:23 +00:00
|
|
|
:p => 6042,
|
|
|
|
:b => '0.0.0.0',
|
2012-08-02 09:17:22 +00:00
|
|
|
:s => false,
|
2012-08-06 06:29:39 +00:00
|
|
|
:d => false,
|
2012-08-02 09:17:22 +00:00
|
|
|
:k => '/path/to/server.key',
|
|
|
|
:c => '/path/to/server.crt',
|
2012-07-23 22:22:23 +00:00
|
|
|
}
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options = {}
|
2012-07-23 22:22:23 +00:00
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: websocket-server.rb [options]"
|
|
|
|
|
2012-08-06 06:29:39 +00:00
|
|
|
opts.on("-d", "--debug", "enable debug messages") do |d|
|
|
|
|
@options[:d] = d
|
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
opts.on("-p", "--port [OPT]", "port of websocket server") do |p|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:p] = p
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
opts.on("-b", "--bind [OPT]", "bind address") do |b|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:b] = b
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2012-08-02 09:17:22 +00:00
|
|
|
opts.on("-s", "--secure", "enable secure connections") do |s|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:s] = s
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
|
|
|
opts.on("-k", "--private-key [OPT]", "/path/to/server.key for secure connections") do |k|
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options[:private_key_file] = k
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
|
|
|
opts.on("-c", "--certificate [OPT]", "/path/to/server.crt for secure connections") do |c|
|
2012-08-02 09:30:30 +00:00
|
|
|
tls_options[:cert_chain_file] = c
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end.parse!
|
|
|
|
|
2012-08-06 06:29:39 +00:00
|
|
|
puts "Starting websocket server on #{ @options[:b] }:#{ @options[:p] } (secure:#{ @options[:s].to_s })"
|
2012-08-02 09:17:22 +00:00
|
|
|
#puts options.inspect
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
@clients = {}
|
|
|
|
EventMachine.run {
|
2012-08-06 06:29:39 +00:00
|
|
|
EventMachine::WebSocket.start( :host => @options[:b], :port => @options[:p], :secure => @options[:s], :tls_options => tls_options ) do |ws|
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
# register client connection
|
|
|
|
ws.onopen {
|
|
|
|
client_id = ws.object_id
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', 'Client connected.', client_id
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
if !@clients.include? client_id
|
|
|
|
@clients[client_id] = {
|
2012-08-07 05:33:47 +00:00
|
|
|
:websocket => ws,
|
|
|
|
:last_ping => Time.new,
|
|
|
|
:error_count => 0,
|
2012-08-06 06:29:39 +00:00
|
|
|
}
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# unregister client connection
|
|
|
|
ws.onclose {
|
|
|
|
client_id = ws.object_id
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', 'Client disconnected.', client_id
|
2012-08-04 13:35:55 +00:00
|
|
|
|
|
|
|
# removed from current client list
|
2012-07-23 22:22:23 +00:00
|
|
|
if @clients.include? client_id
|
|
|
|
@clients.delete client_id
|
|
|
|
end
|
2012-08-04 13:35:55 +00:00
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
Session.destory( client_id )
|
|
|
|
}
|
|
|
|
|
|
|
|
# manage messages
|
|
|
|
ws.onmessage { |msg|
|
|
|
|
|
|
|
|
client_id = ws.object_id
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'debug', "received message: #{ msg } ", client_id
|
|
|
|
begin
|
|
|
|
data = JSON.parse(msg)
|
|
|
|
rescue => e
|
|
|
|
log 'error', "can't parse message: #{ msg }, #{ e.inspect}", client_id
|
|
|
|
next
|
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2012-08-07 05:33:47 +00:00
|
|
|
# check if connection already exists
|
|
|
|
next if !@clients[client_id]
|
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
# get session
|
|
|
|
if data['action'] == 'login'
|
|
|
|
@clients[client_id][:session] = data['session']
|
|
|
|
Session.create( client_id, data['session'] )
|
2012-08-04 13:35:55 +00:00
|
|
|
|
2012-08-06 06:29:39 +00:00
|
|
|
# remember ping, send pong back
|
2012-08-04 13:35:55 +00:00
|
|
|
elsif data['action'] == 'ping'
|
|
|
|
@clients[client_id][:last_ping] = Time.now
|
2012-08-06 06:29:39 +00:00
|
|
|
@clients[client_id][:websocket].send( '[{"action":"pong"}]' )
|
2012-08-04 13:35:55 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-08-06 06:29:39 +00:00
|
|
|
# check open unused connections, kick all connection without activitie in the last 5 minutes
|
|
|
|
EventMachine.add_periodic_timer(120) {
|
|
|
|
log 'notice', "check unused idle connections..."
|
|
|
|
@clients.each { |client_id, client|
|
2012-08-07 05:33:47 +00:00
|
|
|
if ( client[:last_ping] + ( 60 * 4 ) ) < Time.now
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'notice', "closing idle connection", client_id
|
|
|
|
|
|
|
|
# remember to not use this connection anymore
|
2012-08-07 05:33:47 +00:00
|
|
|
client[:disconnect] = true
|
2012-08-06 06:29:39 +00:00
|
|
|
|
|
|
|
# try to close regular
|
2012-08-07 05:33:47 +00:00
|
|
|
client[:websocket].close_websocket
|
2012-08-06 06:29:39 +00:00
|
|
|
|
|
|
|
# delete sesstion from client list
|
|
|
|
sleep 1
|
|
|
|
@clients.delete(client_id)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EventMachine.add_periodic_timer(20) {
|
|
|
|
log 'notice', "Status: clients: #{ @clients.size }"
|
|
|
|
@clients.each { |client_id, client|
|
|
|
|
log 'notice', 'working...', client_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 22:46:05 +00:00
|
|
|
EventMachine.add_periodic_timer(0.2) {
|
2012-08-06 06:29:39 +00:00
|
|
|
next if @clients.size == 0
|
|
|
|
log 'debug', "checking for data..."
|
2012-07-23 22:22:23 +00:00
|
|
|
@clients.each { |client_id, client|
|
2012-08-07 05:33:47 +00:00
|
|
|
next if client[:disconnect]
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'debug', 'checking for data...', client_id
|
2012-07-23 22:22:23 +00:00
|
|
|
begin
|
|
|
|
queue = Session.queue( client_id )
|
|
|
|
if queue && queue[0]
|
2012-08-03 22:46:05 +00:00
|
|
|
# log "send " + queue.inspect, client_id
|
2012-08-06 06:29:39 +00:00
|
|
|
log 'debug', "send data to client", client_id
|
2012-07-23 22:22:23 +00:00
|
|
|
client[:websocket].send( queue.to_json )
|
|
|
|
end
|
2012-08-03 22:46:05 +00:00
|
|
|
rescue => e
|
2012-08-06 06:29:39 +00:00
|
|
|
|
|
|
|
log 'error', 'problem:' + e.inspect, client_id
|
|
|
|
|
|
|
|
# disconnect client
|
2012-08-07 05:33:47 +00:00
|
|
|
client[:error_count] += 1
|
|
|
|
if client[:error_count] > 100
|
|
|
|
if @clients.include? client_id
|
|
|
|
@clients.delete client_id
|
|
|
|
end
|
2012-08-06 06:29:39 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 22:46:05 +00:00
|
|
|
|
2012-08-06 06:29:39 +00:00
|
|
|
def log( level, data, client_id = '-' )
|
|
|
|
if !@options[:d]
|
|
|
|
return if level == 'debug'
|
|
|
|
end
|
2012-08-03 22:46:05 +00:00
|
|
|
puts "#{Time.now}:client(#{ client_id }) #{ data }"
|
2012-08-06 06:29:39 +00:00
|
|
|
# puts "#{Time.now}:#{ level }:client(#{ client_id }) #{ data }"
|
2012-08-03 22:46:05 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
|
|
|
|
}
|