class Citrus::Connectors::WsConnector
Public Class Methods
new(port, host, args={})
click to toggle source
Create a new websocket connector
@param [Integer] port @param [String] host @param [Hash] args
# File lib/citrus/connectors/ws_connector.rb, line 25 def initialize port, host, args={} @port = port @host = host @args = args @heartbeats = args[:heartbeats] || true @heartbeat_timeout = args[:heartbeat_timeout] || 0.06 @heartbeat_interval = args[:heartbeat_interval] || 0.025 @cur_id = 0 end
Public Instance Methods
decode(msg)
click to toggle source
Decode message
@param [String] msg
# File lib/citrus/connectors/ws_connector.rb, line 79 def decode msg begin JSON.parse msg rescue => err end end
encode(req_id, route, msg)
click to toggle source
Encode message
@param [Integer, NilClass] req_id @param [String] route @param [Object] msg
# File lib/citrus/connectors/ws_connector.rb, line 68 def encode req_id, route, msg if req_id compose_response req_id, route, msg else componse_push route, msg end end
start() { || ... }
click to toggle source
Start the connector to listen to the specified port
# File lib/citrus/connectors/ws_connector.rb, line 38 def start &block begin @server = WebSocket::EventMachine::Server.start(:host => @host, :port => @port) { |ws| ws.onopen { ws_socket = WsSocket.new @cur_id, ws @cur_id += 1 emit :connection, ws_socket ws_socket.on(:closing) { |reason| ws_socket.send({ 'route' => 'on_kick', 'reason' => reason }) } } } rescue => err end EM.next_tick { block_given? and yield } end
stop(force=false) { || ... }
click to toggle source
Stop the connector
@param [Boolean] force
# File lib/citrus/connectors/ws_connector.rb, line 58 def stop force=false, &block @server.close EM.next_tick { block_given? and yield } end
Private Instance Methods
compose_push(route, msg_body)
click to toggle source
Compose push message
@param [String] route @param [Hash] msg_body
@private
# File lib/citrus/connectors/ws_connector.rb, line 105 def compose_push route, msg_body { 'route' => route, 'body' => msg_body } end
compose_response(msg_id, route, msg_body)
click to toggle source
Compose response message
@param [Integer] msg_id @param [String] route @param [Hash] msg_body
@private
# File lib/citrus/connectors/ws_connector.rb, line 95 def compose_response msg_id, route, msg_body { 'id' => msg_id, 'body' => msg_body } end