From 484b5f7496dc84d0eaa40db84087a19bfd55de52 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Sun, 26 Aug 2012 22:00:03 -0500 Subject: [PATCH] * all messages are finally binary encoded --- bin/websocket_gateway | 2 +- lib/jam_websockets/router.rb | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bin/websocket_gateway b/bin/websocket_gateway index 8087fdb46..789c35f1d 100755 --- a/bin/websocket_gateway +++ b/bin/websocket_gateway @@ -22,4 +22,4 @@ end Logging.logger.root.appenders = Logging.appenders.stdout ActiveRecord::Base.establish_connection(db_config) -Server.new.run :port => config["port"], :debug => true# config["debug"] +Server.new.run :port => config["port"], :emwebsocket_debug => config["emwebsocket_debug"] diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index 5150fa918..de5b54770 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -129,9 +129,9 @@ module JamWebsockets @log.debug "received user-directed message for session: #{user_id}" contexts.each do |context| - @log.debug "sending user message to #{context}" EM.schedule do - context.client.instance_variable_get(:@handler).send_frame(:binary, msg) + @log.debug "sending user message to #{context}" + send_to_client(context.client, msg) end end end @@ -161,11 +161,9 @@ module JamWebsockets @log.debug "received session-directed message for session: #{session_id}" contexts.each do |context| - @log.debug "sending session message to #{context}" EM.schedule do - @log.debug "ONTUHNOTEHU" - context.client.instance_variable_get(:@handler).send_frame(:binary, msg) - @log.debug "gross" + @log.debug "sending session message to #{context}" + send_to_client(context.client, msg) end end end @@ -177,6 +175,11 @@ module JamWebsockets end end + def send_to_client(client, msg) + # this is so odd that this is necessary. but searching through the source code... it's all I could find in em-websocket + client.instance_variable_get(:@handler).send_frame(:binary, msg) + end + def cleanup() # shutdown topic listeners and mq connection begin @@ -257,7 +260,8 @@ module JamWebsockets @log.info "ending client session deliberately due to malformed client behavior. reason=#{e}" begin # wrap the message up and send it down - client.send(@message_factory.server_rejection_error(e.to_s).to_s) + error_msg = @message_factory.server_rejection_error(e.to_s).to_s + send_to_client(client, error_msg) ensure client.close_websocket cleanup_client(client) @@ -268,7 +272,8 @@ module JamWebsockets begin # wrap the message up and send it down - client.send(@message_factory.server_generic_error(e.to_s).to_s) + error_msg = @message_factory.server_generic_error(e.to_s).to_s + send_to_client(client, error_msg) ensure client.close_websocket cleanup_client(client) @@ -374,7 +379,8 @@ module JamWebsockets @log.debug "user #{user.email} logged in" # respond with LOGIN_ACK to let client know it was successful - client.send(@message_factory.login_ack(client.request["origin"]).to_s) + login_ack = @message_factory.login_ack(client.request["origin"]).to_s + send_to_client(client, login_ack) # remove from pending_queue @semaphore.synchronize do @@ -415,13 +421,15 @@ module JamWebsockets rescue => e # send back a failure ack and bail @log.debug "client requested non-existent session. client:#{client.request['origin']} user:#{context.user.email}" - client.send(@message_factory.login_jam_session_ack(true, e.to_s).to_s) + login_jam_session = @message_factory.login_jam_session_ack(true, e.to_s).to_s + send_to_client(client, login_jam_session) return end # respond with LOGIN_JAM_SESSION_ACK to let client know it was successful - client.send(@message_factory.login_jam_session_ack(false, nil).to_s) - + login_jam_session = @message_factory.login_jam_session_ack(false, nil).to_s + send_to_client(client, login_jam_session) + # send 'new client' message to other members in the session handle_session_directed(session_id, @message_factory.user_joined_jam_session(context.user.id, context.user.name),