Esempio n. 1
0
        public async Task ConnectAsync(int roomId, CancellationToken?cancellationToken = null)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("");
            }
            m_client   = new ClientWebSocket();
            m_innerRts = new CancellationTokenSource();
            string  BB = MMPU.返回网页内容_GET("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=" + roomId, 20000);
            JObject JO = (JObject)JsonConvert.DeserializeObject(BB);

            try
            {
                foreach (var item in JO["data"]["host_list"])
                {
                    if (!bilibili.已经使用的服务器组.Contains(item["host"].ToString()))
                    {
                        wss_S = item["host"].ToString();
                        bilibili.已经使用的服务器组.Add(wss_S);
                        break;
                    }
                }
                if (string.IsNullOrEmpty(wss_S))
                {
                    wss_S = JO["data"]["host_list"][1]["host"].ToString();
                    bilibili.已经使用的服务器组.Add(wss_S);
                }
                await m_client.ConnectAsync(new Uri("wss://" + wss_S + "/sub"), cancellationToken ?? new CancellationTokenSource(30000).Token);

                //await m_client.ConnectAsync(new Uri("wss://broadcastlv.chat.bilibili.com/sub"), cancellationToken ?? new CancellationTokenSource(300000).Token);
            }
            catch (Exception e)
            {
                InfoLog.InfoPrintf("WSS连接发生错误:" + e.ToString(), InfoLog.InfoClass.Debug);
                //Console.WriteLine(e.ToString());
            }

            int realRoomId = roomId;//await _getRealRoomId(roomId);

            await _sendObject(7, new
            {
                uid       = 0,
                roomid    = realRoomId,
                protover  = 2,
                platform  = "web",
                clientver = "1.7.3",
                type      = "1",
                key       = JO["data"]["token"].ToString()
            });

            _ = _innerLoop().ContinueWith((t) =>
            {
                if (t.IsFaulted)
                {
                    //UnityEngine.Debug.LogError(t.Exception.);
                    if (!m_innerRts.IsCancellationRequested)
                    {
                        MessageReceived(this, new ExceptionEventArgs(t.Exception.InnerException));
                        m_innerRts.Cancel();
                    }
                }
                else
                {
                    //POST-CANCEL
                    InfoLog.InfoPrintf("LiveChatListener连接断开,房间号:" + realRoomId, InfoLog.InfoClass.Debug);
                    //Console.WriteLine("LiveChatListender cancelled.");
                }
                try
                {
                    m_client.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait();
                }
                catch (Exception)
                {
                    Dispose();
                }
            });
            _ = _innerHeartbeat();
        }