Class: Vox::Gateway::WebSocket

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/vox/gateway/websocket.rb

Overview

Websocket that handles data interchange for Client.

Constant Summary collapse

ZLIB_SUFFIX =

Zlib boundary used for separating messages split into multiple frames.

"\x00\x00\xFF\xFF".b.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, port: nil, compression: true) ⇒ WebSocket

Returns a new instance of WebSocket.



24
25
26
27
28
29
# File 'lib/vox/gateway/websocket.rb', line 24

def initialize(url, port: nil, compression: true)
  @url = url
  @uri = URI.parse(url)
  @port = port || @uri.scheme == 'wss' ? 443 : 80
  @inflate = Zlib::Inflate.new if compression
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



22
23
24
# File 'lib/vox/gateway/websocket.rb', line 22

def driver
  @driver
end

#threadObject (readonly)

Returns the value of attribute thread.



22
23
24
# File 'lib/vox/gateway/websocket.rb', line 22

def thread
  @thread
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/vox/gateway/websocket.rb', line 22

def url
  @url
end

Instance Method Details

#close(reason = nil, code = 1000) ⇒ true, false

Close the websocket connection.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for closing the websocket.

  • code (Integer) (defaults to: 1000)

    The code to close the websocket with.

Returns:

  • (true, false)

    Whether the websocket closed successfully.



99
100
101
# File 'lib/vox/gateway/websocket.rb', line 99

def close(reason = nil, code = 1000)
  @driver.close(reason, code)
end

#connectThread

Connect to the websocket server.

Returns:

  • (Thread)

    The thread handling the read loop.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vox/gateway/websocket.rb', line 44

def connect
  # Flush the zlib buffer
  @inflate&.reset

  # Create a socket connection to the URL
  @socket = create_socket

  # Initialize the websocket driver
  setup_driver

  # Read until our websocket closes.
  @thread = Thread.new do
    read_loop
  end
end

#on('open', &block) ⇒ Object #on('message') {|data| ... } ⇒ Object #on('close') {|code, reason| ... } ⇒ Object

Overloads:

  • #on('open', &block) ⇒ Object

    Emitted when the websocket finishes its connecting process.

  • #on('message') {|data| ... } ⇒ Object

    Emitted when a message is parsed from the websocket.

    Yield Parameters:

    • data (String)

      The received message.

  • #on('close') {|code, reason| ... } ⇒ Object

    Emitted when the websocket connection closes.

    Yield Parameters:

    • code (Integer)

      The given close code.

    • reason (String, nil)

      The reason the websocket was closed.



# File 'lib/vox/gateway/websocket.rb', line 31

#send(message) ⇒ true, false

Send a ‘text` message.

Parameters:

  • message (String)

    The ‘text` message to write to the websocket.

Returns:

  • (true, false)

    Whether the message was sent successfully.



63
64
65
66
# File 'lib/vox/gateway/websocket.rb', line 63

def send(message)
  LOGGER.debug { "[OUT] #{message} " }
  @driver.text(message)
end

#send_binary(data) ⇒ true, false

Send a ‘binary` frame.

Parameters:

  • data (String)

    The binary data to write to the websocket.

Returns:

  • (true, false)

    Whether the data was send successfully.



79
80
81
# File 'lib/vox/gateway/websocket.rb', line 79

def send_binary(data)
  @driver.binary(data)
end

#send_json(hash) ⇒ true, false

Serialize a hash to send as a ‘text` message.

Parameters:

  • hash (Hash)

    The hash to serialize and send as a ‘text` message.

Returns:

  • (true, false)

    Whether the message was sent successfully.



71
72
73
74
# File 'lib/vox/gateway/websocket.rb', line 71

def send_json(hash)
  data = MultiJson.dump(hash)
  send(data)
end