class Citrus::Connectors::WsSocket

WsSocket

Attributes

id[R]
remote_address[R]

Public Class Methods

new(id, ws) click to toggle source

Create a new ws socket

@param [Integer] id @param [Object] ws

# File lib/citrus/connectors/ws_socket.rb, line 24
def initialize id, ws
  @id = id
  @ws = ws

  port, ip = Socket.unpack_sockaddr_in @ws.get_peername
  @remote_address = {
    :port => port,
    :ip => ip
  }

  @ws.onclose { emit :disconnect }
  @ws.onerror { |err| emit :error }
  @ws.onmessage { |msg, type| emit :message, msg }

  @state = :state_inited
end

Public Instance Methods

disconnect() click to toggle source

Disconnect the client

# File lib/citrus/connectors/ws_socket.rb, line 50
def disconnect
  return if @state == :state_closed
  @state = :state_closed
  @ws.close
end
send(msg) click to toggle source

Send message to the client

@param [Hash] msg

# File lib/citrus/connectors/ws_socket.rb, line 44
def send msg
  return unless @state == :state_inited
  @ws.send msg.to_json
end
send_batch(msgs) click to toggle source

Batch version for send

@param [Array] msgs

# File lib/citrus/connectors/ws_socket.rb, line 59
def send_batch msgs
  @ws.send encode_batch(msgs)
end

Private Instance Methods

encode_batch(msgs) click to toggle source

Encode batch messages

@param [Array] msgs

@private

# File lib/citrus/connectors/ws_socket.rb, line 70
def encode_batch msgs
  msgs.to_json
end