Class: Rubycord::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/websocket.rb

Overview

Utility wrapper class that abstracts an instance of WSCS. Useful should we decide that WSCS isn't good either - in that case we can just switch to something else

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, open_handler, message_handler, close_handler, error_handler) ⇒ WebSocket

Create a new WebSocket and connect to the given endpoint.

Parameters:

  • endpoint (String)

    Where to connect to.

  • open_handler (#call)

    The handler that should be called when the websocket has opened successfully.

  • message_handler (#call)

    The handler that should be called when the websocket receives a message. The handler can take one parameter which will have a data attribute for normal messages and code and data for close frames.

  • close_handler (#call)

    The handler that should be called when the websocket is closed due to an internal error. The error will be passed as the first parameter to the handler.

  • error_handler (#call)

    The handler that should be called when an error occurs in another handler. The error will be passed as the first parameter to the handler.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubycord/websocket.rb', line 29

def initialize(endpoint, open_handler, message_handler, close_handler, error_handler)
  Rubycord::LOGGER.debug "Using WSCS version: #{::WebSocket::Client::Simple::VERSION}"

  @open_handler = open_handler
  @message_handler = message_handler
  @close_handler = close_handler
  @error_handler = error_handler

  instance = self # to work around WSCS's weird way of handling blocks

  @client = ::WebSocket::Client::Simple.connect(endpoint) do |ws|
    ws.on(:open) { instance.open_handler.call }
    ws.on(:message) do |msg|
      # If the message has a code attribute, it is in reality a close message
      if msg.code
        instance.close_handler.call(msg)
      else
        instance.message_handler.call(msg.data)
      end
    end
    ws.on(:close) { |err| instance.close_handler.call(err) }
    ws.on(:error) { |err| instance.error_handler.call(err) }
  end
end

Instance Attribute Details

#close_handlerObject (readonly)

Returns the value of attribute close_handler.



17
18
19
# File 'lib/rubycord/websocket.rb', line 17

def close_handler
  @close_handler
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



17
18
19
# File 'lib/rubycord/websocket.rb', line 17

def error_handler
  @error_handler
end

#message_handlerObject (readonly)

Returns the value of attribute message_handler.



17
18
19
# File 'lib/rubycord/websocket.rb', line 17

def message_handler
  @message_handler
end

#open_handlerObject (readonly)

Returns the value of attribute open_handler.



17
18
19
# File 'lib/rubycord/websocket.rb', line 17

def open_handler
  @open_handler
end

Instance Method Details

#closeObject

Close the WebSocket connection



61
62
63
# File 'lib/rubycord/websocket.rb', line 61

def close
  @client.close
end

#send(data) ⇒ Object

Send data over this WebSocket

Parameters:

  • data (String)

    What to send



56
57
58
# File 'lib/rubycord/websocket.rb', line 56

def send(data)
  @client.send(data)
end

#threadThread

Returns the internal WSCS thread.

Returns:

  • (Thread)

    the internal WSCS thread



66
67
68
# File 'lib/rubycord/websocket.rb', line 66

def thread
  @client.thread
end