Class: WSClient
- Inherits:
-
Object
- Object
- WSClient
- Defined in:
- lib/chatx/websocket.rb
Instance Attribute Summary collapse
-
#dead ⇒ Object
readonly
Returns the value of attribute dead.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#handler ⇒ Object
Returns the value of attribute handler.
-
#in_rooms ⇒ Object
readonly
Returns the value of attribute in_rooms.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, cookies, bot, server, message_log: 'ws_messages.log', error_log: 'ws_errors.log', info_log: 'ws_info.log', dev_log: 'ws_dev.log') ⇒ WSClient
constructor
A new instance of WSClient.
- #send(message) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(url, cookies, bot, server, message_log: 'ws_messages.log', error_log: 'ws_errors.log', info_log: 'ws_info.log', dev_log: 'ws_dev.log') ⇒ WSClient
Returns a new instance of WSClient.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/chatx/websocket.rb', line 5 def initialize(url, , bot, server, message_log: 'ws_messages.log', error_log: 'ws_errors.log', info_log: 'ws_info.log', dev_log: 'ws_dev.log') # Setup loggers @message_logger = Logger.new() if @error_logger = Logger.new(error_log) if error_log @info_logger = Logger.new(info_log) if info_log @dev_logger = Logger.new(dev_log) if dev_log @bot = bot @in_rooms = {rooms: [], last_update: Time.now} @server = server # Get WS(S) URI @uri = URI.parse(url) @url = "ws#{@uri.scheme.split("")[4]}://#{@uri.host}#{@uri.path}?#{@uri.query}" # Setup handler (the action that's executed on message) @handler = handler # Auto-restart unless you don't @restart = true @dev_logger.info "Set @restart to #{@restart}" setup(cookies: , origin: "https://chat.#{@server}.com") end |
Instance Attribute Details
#dead ⇒ Object (readonly)
Returns the value of attribute dead.
2 3 4 |
# File 'lib/chatx/websocket.rb', line 2 def dead @dead end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
2 3 4 |
# File 'lib/chatx/websocket.rb', line 2 def driver @driver end |
#handler ⇒ Object
Returns the value of attribute handler.
3 4 5 |
# File 'lib/chatx/websocket.rb', line 3 def handler @handler end |
#in_rooms ⇒ Object (readonly)
Returns the value of attribute in_rooms.
2 3 4 |
# File 'lib/chatx/websocket.rb', line 2 def in_rooms @in_rooms end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
2 3 4 |
# File 'lib/chatx/websocket.rb', line 2 def thread @thread end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
2 3 4 |
# File 'lib/chatx/websocket.rb', line 2 def url @url end |
Instance Method Details
#close ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/chatx/websocket.rb', line 39 def close @info_logger.warn "Was told to close. Was sad." @dead = true @driver.close @socket.is_a?(TCPSocket) ? @socket.shutdown : @socket.sysclose @info_logger.info "Closed sucessfully" rescue IOError, Errno::ENOTCONN => e @logger.error "Recieved #{e.class} trying to close websocket. Ignoring..." end |
#send(message) ⇒ Object
30 31 32 33 |
# File 'lib/chatx/websocket.rb', line 30 def send() @message_logger.info() @driver.text() end |
#write(data) ⇒ Object
35 36 37 |
# File 'lib/chatx/websocket.rb', line 35 def write(data) @socket.write(data) end |