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.

4

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)

Parameters:

  • channel (Channel)

    The voice channel to connect to

  • bot (Bot)

    The regular bot to which this vWS is bound

  • token (String)

    The authentication token which is also used for REST requests

  • session (String)

    The voice session ID Discord sends over the regular websocket

  • endpoint (String)

    The endpoint URL to connect to



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/discordrb/voice/network.rb', line 181

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.split(':').first

  @udp = VoiceUDP.new
end

Instance Attribute Details

#udpVoiceUDP (readonly)

Returns the UDP voice connection over which the actual audio data is sent.

Returns:

  • (VoiceUDP)

    the UDP voice connection over which the actual audio data is sent.



173
174
175
# File 'lib/discordrb/voice/network.rb', line 173

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 -> | | | …



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/discordrb/voice/network.rb', line 307

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



330
331
332
# File 'lib/discordrb/voice/network.rb', line 330

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



230
231
232
233
234
235
236
237
238
# File 'lib/discordrb/voice/network.rb', line 230

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

  @client.send({
    op: 3,
    d: millis
  }.to_json)
end

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

Send a connection init packet (op 0)

Parameters:

  • server_id (Integer)

    The ID of the server to connect to

  • bot_user_id (Integer)

    The ID of the bot that is connecting

  • session_id (String)

    The voice session ID

  • token (String)

    The Discord authentication token



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/discordrb/voice/network.rb', line 199

def send_init(server_id, bot_user_id, session_id, token)
  @client.send({
    op: 0,
    d: {
      server_id: server_id,
      user_id: bot_user_id,
      session_id: session_id,
      token: token
    }
  }.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

Parameters:

  • value (true, false, Integer)

    Whether or not the bot should be speaking, can also be a bitmask denoting audio type.



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

def send_speaking(value)
  @bot.debug("Speaking: #{value}")
  @client.send({
    op: 5,
    d: {
      speaking: value,
      delay: 0
    }
  }.to_json)
end

#send_udp_connection(ip, port, mode) ⇒ Object

Sends the UDP connection packet (op 1)

Parameters:

  • ip (String)

    The IP to bind UDP to

  • port (Integer)

    The port to bind UDP to

  • mode (Object)

    Which mode to use for the voice connection



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/discordrb/voice/network.rb', line 215

def send_udp_connection(ip, port, mode)
  @client.send({
    op: 1,
    d: {
      protocol: 'udp',
      data: {
        address: ip,
        port: port,
        mode: mode
      }
    }
  }.to_json)
end