Class: Citrus::Connectors::WsSocket
- Inherits:
-
Object
- Object
- Citrus::Connectors::WsSocket
- Includes:
- Utils::EventEmitter
- Defined in:
- lib/citrus/connectors/ws_socket.rb
Overview
WsSocket
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#remote_address ⇒ Object
readonly
Returns the value of attribute remote_address.
Instance Method Summary collapse
-
#disconnect ⇒ Object
Disconnect the client.
-
#initialize(id, ws) ⇒ WsSocket
constructor
Create a new ws socket.
-
#send(msg) ⇒ Object
Send message to the client.
-
#send_batch(msgs) ⇒ Object
Batch version for send.
Methods included from Utils::EventEmitter
Constructor Details
#initialize(id, ws) ⇒ WsSocket
Create a new ws socket
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. { |msg, type| emit :message, msg } @state = :state_inited end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
18 19 20 |
# File 'lib/citrus/connectors/ws_socket.rb', line 18 def id @id end |
#remote_address ⇒ Object (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
#disconnect ⇒ Object
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
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
59 60 61 |
# File 'lib/citrus/connectors/ws_socket.rb', line 59 def send_batch msgs @ws.send encode_batch(msgs) end |