Moved from integers to strings for client_id's for keep code simpler.

This commit is contained in:
Martin Edenhofer 2013-06-22 19:00:58 +02:00
parent f921df1045
commit 35e12c58a5
3 changed files with 10 additions and 9 deletions

View file

@ -67,7 +67,7 @@ class LongPollingController < ApplicationController
# list all current clients # list all current clients
client_list = Session.list client_list = Session.list
client_list.each {|local_client_id, local_client| client_list.each {|local_client_id, local_client|
if local_client_id.to_s != client_id.to_s if local_client_id != client_id
# broadcast to recipient list # broadcast to recipient list
if params['data']['data']['recipient'] && params['data']['data']['recipient']['user_id'] if params['data']['data']['recipient'] && params['data']['data']['recipient']['user_id']
@ -139,11 +139,11 @@ class LongPollingController < ApplicationController
private private
def client_id_check def client_id_check
return params[:client_id] if params[:client_id] return params[:client_id].to_s if params[:client_id]
return return
end end
def client_id_gen def client_id_gen
rand(99999999) rand(9999999999).to_s
end end
def client_id_verify def client_id_verify
return if !params[:client_id] return if !params[:client_id]

View file

@ -257,7 +257,7 @@ module Session
data = [] data = []
Dir.foreach( path ) do |entry| Dir.foreach( path ) do |entry|
next if entry == '.' || entry == '..' || entry == 'spool' next if entry == '.' || entry == '..' || entry == 'spool'
data.push entry data.push entry.to_s
end end
return data return data
end end

View file

@ -89,8 +89,9 @@ EventMachine.run {
# register client connection # register client connection
ws.onopen { ws.onopen {
client_id = ws.object_id client_id = ws.object_id.to_s
log 'notice', 'Client connected.', client_id log 'notice', 'Client connected.', client_id
Session.create( client_id, {}, { :type => 'websocket' } )
if !@clients.include? client_id if !@clients.include? client_id
@clients[client_id] = { @clients[client_id] = {
@ -103,7 +104,7 @@ EventMachine.run {
# unregister client connection # unregister client connection
ws.onclose { ws.onclose {
client_id = ws.object_id client_id = ws.object_id.to_s
log 'notice', 'Client disconnected.', client_id log 'notice', 'Client disconnected.', client_id
# removed from current client list # removed from current client list
@ -117,7 +118,7 @@ EventMachine.run {
# manage messages # manage messages
ws.onmessage { |msg| ws.onmessage { |msg|
client_id = ws.object_id client_id = ws.object_id.to_s
log 'debug', "received message: #{ msg } ", client_id log 'debug', "received message: #{ msg } ", client_id
begin begin
data = JSON.parse(msg) data = JSON.parse(msg)
@ -174,7 +175,7 @@ EventMachine.run {
# list all current clients # list all current clients
client_list = Session.list client_list = Session.list
client_list.each {|local_client_id, local_client| client_list.each {|local_client_id, local_client|
if local_client_id.to_s != client_id.to_s if local_client_id != client_id
# broadcast to recipient list # broadcast to recipient list
if data['data']['recipient'] if data['data']['recipient']
if data['data']['recipient'].class != Hash if data['data']['recipient'].class != Hash
@ -269,7 +270,7 @@ EventMachine.run {
# disconnect client # disconnect client
client[:error_count] += 1 client[:error_count] += 1
if client[:error_count] > 100 if client[:error_count] > 20
if @clients.include? client_id if @clients.include? client_id
@clients.delete client_id @clients.delete client_id
end end