Method: Discordrb::WebSocket#initialize
- Defined in:
- lib/discordrb/websocket.rb
#initialize(endpoint, open_handler, message_handler, close_handler, error_handler) ⇒ WebSocket
Create a new WebSocket and connect to the given endpoint.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/discordrb/websocket.rb', line 31 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 |