Class: Discordrb::Voice::VoiceWS

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/voice/network.rb

Overview

Represents a websocket client connection to the voice server. The websocket connection (sometimes called vWS) is used to manage general data about the connection, such as sending the speaking packet, which determines the green circle around users on Discord, and obtaining UDP connection info.

Constant Summary collapse

VOICE_GATEWAY_VERSION =

The version of the voice gateway that's supposed to be used.

8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, bot, token, session, endpoint) ⇒ VoiceWS

Makes a new voice websocket client, but doesn't connect it (see #connect for that)



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/discordrb/voice/network.rb', line 176

def initialize(channel, bot, token, session, endpoint)
  raise 'libsodium is unavailable - unable to create voice bot! Please read https://github.com/shardlab/discordrb/wiki/Installing-libsodium' unless LIBSODIUM_AVAILABLE

  @channel = channel
  @bot = bot
  @token = token
  @session = session

  @endpoint = endpoint

  @udp = VoiceUDP.new
end

Instance Attribute Details

#udpVoiceUDP (readonly)



168
169
170
# File 'lib/discordrb/voice/network.rb', line 168

def udp
  @udp
end

Instance Method Details

#connectObject

Communication goes like this: me discord | | websocket connect -> | | | | <- websocket opcode 2 | | UDP discovery -> | | | | <- UDP reply packet | | websocket opcode 1 -> | | | ...



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/discordrb/voice/network.rb', line 319

def connect
  # Connect websocket
  @thread = Thread.new do
    Thread.current[:discordrb_name] = 'vws'
    init_ws
  end

  @bot.debug('Started websocket initialization, now waiting for UDP discovery reply')

  # Now wait for opcode 2 and the resulting UDP reply packet
  ip, port = @udp.receive_discovery_reply
  @bot.debug("UDP discovery reply received! #{ip} #{port}")

  # Send UDP init packet with received UDP data
  send_udp_connection(ip, port, @udp_mode)

  @bot.debug('Waiting for op 4 now')

  # Wait for op 4, then finish
  sleep 0.05 until @ready
end

#destroyObject

Disconnects the websocket and kills the thread



342
343
344
# File 'lib/discordrb/voice/network.rb', line 342

def destroy
  @heartbeat_running = false
end

#send_heartbeatObject

Send a heartbeat (op 3), has to be done every @heartbeat_interval seconds or the connection will terminate



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/discordrb/voice/network.rb', line 225

def send_heartbeat
  millis = Time.now.strftime('%s%L').to_i
  @bot.debug("Sending voice heartbeat at #{millis}")

  send_opcode(
    Opcodes::HEARTBEAT,
    {
      t: millis,
      seq_ack: @seq
    }
  )
end

#send_init(server_id, bot_user_id, session_id, token) ⇒ Object

Send a connection init packet (op 0)



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/discordrb/voice/network.rb', line 194

def send_init(server_id, bot_user_id, session_id, token)
  send_opcode(
    Opcodes::IDENTIFY,
    {
      server_id: server_id,
      user_id: bot_user_id,
      session_id: session_id,
      token: token
    }
  )
end

#send_opcode(opcode, data) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/discordrb/voice/network.rb', line 251

def send_opcode(opcode, data)
  @bot.debug("Sending voice opcode #{opcode} with data: #{data}")
  @client.send({
    op: opcode,
    d: data
  }.to_json)
end

#send_speaking(value) ⇒ Object

Send a speaking packet (op 5). This determines the green circle around the avatar in the voice channel



240
241
242
243
244
245
246
247
248
249
# File 'lib/discordrb/voice/network.rb', line 240

def send_speaking(value)
  @bot.debug("Speaking: #{value}")
  send_opcode(
    Opcodes::SPEAKING,
    {
      speaking: value,
      delay: 0
    }
  )
end

#send_udp_connection(ip, port, mode) ⇒ Object

Sends the UDP connection packet (op 1)



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/discordrb/voice/network.rb', line 210

def send_udp_connection(ip, port, mode)
  send_opcode(
    Opcodes::SELECT_PROTOCOL,
    {
      protocol: 'udp',
      data: {
        address: ip,
        port: port,
        mode: mode
      }
    }
  )
end