Class: Citrus::Connectors::WsSocket

Inherits:
Object
  • Object
show all
Includes:
Utils::EventEmitter
Defined in:
lib/citrus/connectors/ws_socket.rb

Overview

WsSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::EventEmitter

#emit, #on, #once

Constructor Details

#initialize(id, ws) ⇒ WsSocket

Create a new ws socket

Parameters:

  • id (Integer)
  • ws (Object)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/citrus/connectors/ws_socket.rb', line 18

def id
  @id
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



18
19
20
# File 'lib/citrus/connectors/ws_socket.rb', line 18

def remote_address
  @remote_address
end

Instance Method Details

#disconnectObject

Disconnect the client



50
51
52
53
54
# File 'lib/citrus/connectors/ws_socket.rb', line 50

def disconnect
  return if @state == :state_closed
  @state = :state_closed
  @ws.close
end

#send(msg) ⇒ Object

Send message to the client

Parameters:

  • msg (Hash)


44
45
46
47
# 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) ⇒ Object

Batch version for send

Parameters:

  • msgs (Array)


59
60
61
# File 'lib/citrus/connectors/ws_socket.rb', line 59

def send_batch msgs
  @ws.send encode_batch(msgs)
end