Class: Discordrb::Voice::VoiceWS
- Inherits:
-
Object
- Object
- Discordrb::Voice::VoiceWS
- 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.
Instance Attribute Summary collapse
-
#udp ⇒ VoiceUDP
readonly
The UDP voice connection over which the actual audio data is sent.
Instance Method Summary collapse
-
#connect ⇒ Object
Communication goes like this: me discord | | websocket connect -> | | | | <- websocket opcode 2 | | UDP discovery -> | | | | <- UDP reply packet | | websocket opcode 1 -> | | | ...
-
#destroy ⇒ Object
Disconnects the websocket and kills the thread.
-
#initialize(channel, bot, token, session, endpoint) ⇒ VoiceWS
constructor
Makes a new voice websocket client, but doesn't connect it (see #connect for that).
-
#send_heartbeat ⇒ Object
Send a heartbeat (op 3), has to be done every @heartbeat_interval seconds or the connection will terminate.
-
#send_init(server_id, bot_user_id, session_id, token) ⇒ Object
Send a connection init packet (op 0).
-
#send_speaking(value) ⇒ Object
Send a speaking packet (op 5).
-
#send_udp_connection(ip, port, mode) ⇒ Object
Sends the UDP connection packet (op 1).
Constructor Details
#initialize(channel, bot, token, session, endpoint) ⇒ VoiceWS
Makes a new voice websocket client, but doesn't connect it (see #connect for that)
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/discordrb/voice/network.rb', line 115 def initialize(channel, bot, token, session, endpoint) @channel = channel @bot = bot @token = token @session = session @endpoint = endpoint @endpoint.gsub!(':80', '') @udp = VoiceUDP.new end |
Instance Attribute Details
#udp ⇒ VoiceUDP (readonly)
Returns the UDP voice connection over which the actual audio data is sent.
107 108 109 |
# File 'lib/discordrb/voice/network.rb', line 107 def udp @udp end |
Instance Method Details
#connect ⇒ Object
Communication goes like this: me discord | | websocket connect -> | | | | <- websocket opcode 2 | | UDP discovery -> | | | | <- UDP reply packet | | websocket opcode 1 -> | | | ...
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/discordrb/voice/network.rb', line 234 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) end |
#destroy ⇒ Object
Disconnects the websocket and kills the thread
252 253 254 |
# File 'lib/discordrb/voice/network.rb', line 252 def destroy @thread.kill if @thread end |
#send_heartbeat ⇒ Object
Send a heartbeat (op 3), has to be done every @heartbeat_interval seconds or the connection will terminate
163 164 165 166 167 168 169 170 171 |
# File 'lib/discordrb/voice/network.rb', line 163 def send_heartbeat millis = Time.now.strftime('%s%L').to_i @bot.debug("Sending voice heartbeat at #{millis}") @client.send({ op: 3, d: nil }.to_json) end |
#send_init(server_id, bot_user_id, session_id, token) ⇒ Object
Send a connection init packet (op 0)
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/discordrb/voice/network.rb', line 132 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
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/discordrb/voice/network.rb', line 175 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)
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/discordrb/voice/network.rb', line 148 def send_udp_connection(ip, port, mode) @client.send({ op: 1, d: { protocol: 'udp', data: { address: ip, port: port, mode: mode } } }.to_json) end |