35
35
import traceback
36
36
import zlib
37
37
from collections import deque , namedtuple
38
+ from typing import TYPE_CHECKING
38
39
39
40
import aiohttp
40
41
@@ -208,6 +209,9 @@ def ack(self):
208
209
209
210
210
211
class VoiceKeepAliveHandler (KeepAliveHandler ):
212
+ if TYPE_CHECKING :
213
+ ws : DiscordVoiceWebSocket
214
+
211
215
def __init__ (self , * args , ** kwargs ):
212
216
super ().__init__ (* args , ** kwargs )
213
217
self .recent_ack_latencies = deque (maxlen = 20 )
@@ -216,7 +220,10 @@ def __init__(self, *args, **kwargs):
216
220
self .behind_msg = "High socket latency, shard ID %s heartbeat is %.1fs behind"
217
221
218
222
def get_payload (self ):
219
- return {"op" : self .ws .HEARTBEAT , "d" : int (time .time () * 1000 )}
223
+ return {
224
+ "op" : self .ws .HEARTBEAT ,
225
+ "d" : {"t" : int (time .time () * 1000 ), "seq_ack" : self .ws .seq_ack },
226
+ }
220
227
221
228
def ack (self ):
222
229
ack_time = time .perf_counter ()
@@ -784,6 +791,7 @@ def __init__(self, socket, loop, *, hook=None):
784
791
self ._close_code = None
785
792
self .secret_key = None
786
793
self .ssrc_map = {}
794
+ self .seq_ack : int = - 1
787
795
if hook :
788
796
self ._hook = hook
789
797
@@ -804,6 +812,9 @@ async def resume(self):
804
812
"token" : state .token ,
805
813
"server_id" : str (state .server_id ),
806
814
"session_id" : state .session_id ,
815
+ # this seq_ack will allow for us to do buffered resume, which is, receive the
816
+ # lost voice packets while trying to resume the reconnection
817
+ "seq_ack" : self .seq_ack ,
807
818
},
808
819
}
809
820
await self .send_as_json (payload )
@@ -824,7 +835,7 @@ async def identify(self):
824
835
@classmethod
825
836
async def from_client (cls , client , * , resume = False , hook = None ):
826
837
"""Creates a voice websocket for the :class:`VoiceClient`."""
827
- gateway = f"wss://{ client .endpoint } /?v=4 "
838
+ gateway = f"wss://{ client .endpoint } /?v=8 "
828
839
http = client ._state .http
829
840
socket = await http .ws_connect (gateway , compress = 15 )
830
841
ws = cls (socket , loop = client .loop , hook = hook )
@@ -860,14 +871,21 @@ async def client_connect(self):
860
871
await self .send_as_json (payload )
861
872
862
873
async def speak (self , state = SpeakingState .voice ):
863
- payload = {"op" : self .SPEAKING , "d" : {"speaking" : int (state ), "delay" : 0 }}
874
+ payload = {
875
+ "op" : self .SPEAKING ,
876
+ "d" : {
877
+ "speaking" : int (state ),
878
+ "delay" : 0 ,
879
+ },
880
+ }
864
881
865
882
await self .send_as_json (payload )
866
883
867
884
async def received_message (self , msg ):
868
885
_log .debug ("Voice websocket frame received: %s" , msg )
869
886
op = msg ["op" ]
870
887
data = msg .get ("d" )
888
+ self .seq_ack = data .get ("seq" , self .seq_ack )
871
889
872
890
if op == self .READY :
873
891
await self .initial_connection (data )
0 commit comments