Class: Discordrb::Voice::VoiceUDP

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

Overview

Represents a UDP connection to a voice server

Instance Method Summary collapse

Constructor Details

#initializeVoiceUDP

Only creates a socket as the discovery reply may come before the data is initialized.



10
11
12
# File 'lib/discordrb/voice/network.rb', line 10

def initialize
  @socket = UDPSocket.new
end

Instance Method Details

#connect(endpoint, port, ssrc) ⇒ Object

Initializes the data from opcode 2



15
16
17
18
19
20
21
22
23
# File 'lib/discordrb/voice/network.rb', line 15

def connect(endpoint, port, ssrc)
  @endpoint = endpoint
  @endpoint = @endpoint[6..-1] if @endpoint.start_with? 'wss://'
  @endpoint.gsub!(':80', '') # The endpoint may contain a port, we don't want that
  @endpoint = Resolv.getaddress @endpoint

  @port = port
  @ssrc = ssrc
end

#receive_discovery_replyObject



25
26
27
28
29
30
31
# File 'lib/discordrb/voice/network.rb', line 25

def receive_discovery_reply
  # Wait for a UDP message
  message = @socket.recv(70)
  ip = message[4..-3].delete("\0")
  port = message[-2..-1].to_i
  [ip, port]
end

#send_audio(buf, sequence, time) ⇒ Object



33
34
35
36
# File 'lib/discordrb/voice/network.rb', line 33

def send_audio(buf, sequence, time)
  packet = [0x80, 0x78, sequence, time, @ssrc].pack('CCnNN') + buf
  send_packet(packet)
end

#send_discoveryObject



38
39
40
41
42
43
44
# File 'lib/discordrb/voice/network.rb', line 38

def send_discovery
  discovery_packet = [@ssrc].pack('N')

  # Add 66 zeroes so the packet is 70 bytes long
  discovery_packet += "\0" * 66
  send_packet(discovery_packet)
end