Class: Discordrb::WebSocket
- Inherits:
-
Object
- Object
- Discordrb::WebSocket
- Defined in:
- lib/discordrb/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
-
#close_handler ⇒ Object
readonly
Returns the value of attribute close_handler.
-
#error_handler ⇒ Object
readonly
Returns the value of attribute error_handler.
-
#message_handler ⇒ Object
readonly
Returns the value of attribute message_handler.
-
#open_handler ⇒ Object
readonly
Returns the value of attribute open_handler.
Instance Method Summary collapse
-
#close ⇒ Object
Close the WebSocket connection.
-
#initialize(endpoint, open_handler, message_handler, close_handler, error_handler) ⇒ WebSocket
constructor
Create a new WebSocket and connect to the given endpoint.
-
#send(data) ⇒ Object
Send data over this WebSocket.
-
#thread ⇒ Thread
The internal WSCS thread.
Constructor Details
#initialize(endpoint, open_handler, message_handler, close_handler, error_handler) ⇒ WebSocket
Create a new WebSocket and connect to the given endpoint.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/discordrb/websocket.rb', line 21 def initialize(endpoint, open_handler, , close_handler, error_handler) Discordrb::LOGGER.debug "Using WSCS version: #{::WebSocket::Client::Simple::VERSION}" @open_handler = open_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..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_handler ⇒ Object (readonly)
Returns the value of attribute close_handler.
9 10 11 |
# File 'lib/discordrb/websocket.rb', line 9 def close_handler @close_handler end |
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler.
9 10 11 |
# File 'lib/discordrb/websocket.rb', line 9 def error_handler @error_handler end |
#message_handler ⇒ Object (readonly)
Returns the value of attribute message_handler.
9 10 11 |
# File 'lib/discordrb/websocket.rb', line 9 def @message_handler end |
#open_handler ⇒ Object (readonly)
Returns the value of attribute open_handler.
9 10 11 |
# File 'lib/discordrb/websocket.rb', line 9 def open_handler @open_handler end |
Instance Method Details
#close ⇒ Object
Close the WebSocket connection
53 54 55 |
# File 'lib/discordrb/websocket.rb', line 53 def close @client.close end |
#send(data) ⇒ Object
Send data over this WebSocket
48 49 50 |
# File 'lib/discordrb/websocket.rb', line 48 def send(data) @client.send(data) end |
#thread ⇒ Thread
Returns the internal WSCS thread.
58 59 60 |
# File 'lib/discordrb/websocket.rb', line 58 def thread @client.thread end |