Class: WSClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, cookies, 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(message_log) if message_log
  @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: cookies, origin: "https://chat.#{@server}.com")
end

Instance Attribute Details

#deadObject (readonly)

Returns the value of attribute dead.



2
3
4
# File 'lib/chatx/websocket.rb', line 2

def dead
  @dead
end

#driverObject (readonly)

Returns the value of attribute driver.



2
3
4
# File 'lib/chatx/websocket.rb', line 2

def driver
  @driver
end

#handlerObject

Returns the value of attribute handler.



3
4
5
# File 'lib/chatx/websocket.rb', line 3

def handler
  @handler
end

#in_roomsObject (readonly)

Returns the value of attribute in_rooms.



2
3
4
# File 'lib/chatx/websocket.rb', line 2

def in_rooms
  @in_rooms
end

#threadObject (readonly)

Returns the value of attribute thread.



2
3
4
# File 'lib/chatx/websocket.rb', line 2

def thread
  @thread
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/chatx/websocket.rb', line 2

def url
  @url
end

Instance Method Details

#closeObject



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)
  @message_logger.info(message)
  @driver.text(message)
end

#write(data) ⇒ Object



35
36
37
# File 'lib/chatx/websocket.rb', line 35

def write(data)
  @socket.write(data)
end