Class: ESM::Websocket

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/websocket.rb,
lib/esm/websocket/queue.rb,
lib/esm/websocket/server.rb,
lib/esm/websocket/request.rb,
lib/esm/websocket/server_request.rb,
lib/esm/websocket/request/overseer.rb,
lib/esm/websocket/connection/overseer.rb

Defined Under Namespace

Modules: Connection Classes: Queue, Request, Server, ServerRequest

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Websocket

Returns a new instance of Websocket.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/esm/websocket.rb', line 76

def initialize(connection)
  @ready = false
  @connection = connection
  @requests = ESM::Websocket::Queue.new
  @closed = false

  # In dev, for whatever reason, this ping causes all messages to be delayed 15 seconds.
  @ping_timer = EventMachine.add_periodic_timer(10) { ping } if ESM.env.production?

  on_open
end

Class Attribute Details

.connectionsObject (readonly)

Returns the value of attribute connections.



6
7
8
# File 'lib/esm/websocket.rb', line 6

def connections
  @connections
end

.server_idsObject (readonly)

Returns the value of attribute server_ids.



6
7
8
# File 'lib/esm/websocket.rb', line 6

def server_ids
  @server_ids
end

Instance Attribute Details

#connectionObject (readonly)

Public Instance Methods



72
73
74
# File 'lib/esm/websocket.rb', line 72

def connection
  @connection
end

#ready=(value) ⇒ Object (writeonly)

Sets if the server has been sent the post_init package



112
113
114
# File 'lib/esm/websocket.rb', line 112

def ready=(value)
  @ready = value
end

#requestsObject

Public Instance Methods



72
73
74
# File 'lib/esm/websocket.rb', line 72

def requests
  @requests
end

#serverObject (readonly)

Public Instance Methods



72
73
74
# File 'lib/esm/websocket.rb', line 72

def server
  @server
end

Class Method Details

.add_connection(connection) ⇒ Object

Adds a new connection to the connections



37
38
39
40
41
# File 'lib/esm/websocket.rb', line 37

def self.add_connection(connection)
  # Uniquely append to the server IDs array
  @server_ids |= [connection.server.server_id]
  @connections[connection.server.server_id] = connection
end

.connected?(server_id) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/esm/websocket.rb', line 55

def self.connected?(server_id)
  connection(server_id).present?
end

.connection(server_id) ⇒ WebsocketConnection?

Retrieves the WS connection based on a server_id

Parameters:

  • server_id (String)

    The ESM set ID, not the DB ID

Returns:

  • (WebsocketConnection, nil)


63
64
65
66
67
# File 'lib/esm/websocket.rb', line 63

def self.connection(server_id)
  return if @connections.blank?

  @connections[server_id]
end

.deliver!(server_id, request) ⇒ Object

Note:

Do not rescue. This will fall down to the calling class

Delivers the message to the requested server_id



28
29
30
31
32
33
34
# File 'lib/esm/websocket.rb', line 28

def self.deliver!(server_id, request)
  connection = connection(server_id)
  request.command.check_failed!(:server_not_connected, user: request.user.mention, server_id: server_id) if connection.nil?
  request.command.check_failed!(:server_not_initialized, user: request.user.mention, server_id: server_id) if !connection.ready?

  connection.deliver!(request)
end

.remove_all_connections!Object

Removes all connections



51
52
53
# File 'lib/esm/websocket.rb', line 51

def self.remove_all_connections!
  @connections.each { |_server_id, connection| remove_connection(connection) }
end

.remove_connection(connection) ⇒ Object

Removes a connection from the connections



44
45
46
47
48
# File 'lib/esm/websocket.rb', line 44

def self.remove_connection(connection)
  connection.server.update(server_start_time: nil, disconnected_at: ::Time.zone.now) if !ESM.env.test?
  @server_ids.delete(connection.server.server_id)
  @connections.delete(connection.server.server_id)
end

.start!Object

Starts the websocket server and the request watcher thread



14
15
16
17
18
19
20
21
22
23
# File 'lib/esm/websocket.rb', line 14

def self.start!
  @connections = {}
  @server_ids = ESM::Server.all.pluck(:server_id)

  # Start the websocket server
  ESM::Websocket::Server.run

  # Watches over requests and removes them if the server is taking too long to respond
  ESM::Websocket::Request::Overseer.watch!
end

Instance Method Details

#deliver!(request) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/esm/websocket.rb', line 88

def deliver!(request)
  info!(request.to_h)

  @requests << request

  # Send the message
  @connection.send(request.to_s)

  request
end

#pingObject

Sends a ping to the WS client.



115
116
117
# File 'lib/esm/websocket.rb', line 115

def ping
  @connection.ping
end

#ready?Boolean

Returns if the server has been sent the post_init package

Returns:

  • (Boolean)

    boolean



107
108
109
# File 'lib/esm/websocket.rb', line 107

def ready?
  @ready
end

#remove_request(command_id) ⇒ ESM::Websocket::Request?

Removes a request via its commandID

Returns:



101
102
103
# File 'lib/esm/websocket.rb', line 101

def remove_request(command_id)
  @requests.remove(command_id)
end