Exemplo n.º 1
0
        private async void Live2WebSocket_RecieveCurrentRoom(Live2CurrentRoomEventArgs e)
        {
            RoomName = e.RoomName;

            if (e.MessageServerType == "niwavided")
            {
                _NicoLiveCommentClient = new Niwavided.NiwavidedNicoLiveCommentClient(
                    e.MessageServerUrl,
                    e.ThreadId,
                    this.HohoemaApp.LoginUserId.ToString(),
                    HohoemaApp.NiconicoContext.HttpClient
                    );

                _NicoLiveCommentClient.Connected       += _NicoLiveCommentClient_Connected;
                _NicoLiveCommentClient.Disconnected    += _NicoLiveCommentClient_Disconnected;
                _NicoLiveCommentClient.CommentRecieved += _NicoLiveCommentClient_CommentRecieved;
                _NicoLiveCommentClient.CommentPosted   += _NicoLiveCommentClient_CommentPosted;

                // コメントの受信処理と映像のオープンが被ると
                // 自動再生が失敗する?ので回避のため数秒遅らせる

                _NicoLiveCommentClient.Open();

                await Task.Delay(500);

                await RefreshLeoPlayer();
            }
        }
Exemplo n.º 2
0
        private async void Live2WebSocket_RecieveCurrentRoom(Live2CurrentRoomEventArgs e)
        {
            RoomName = e.RoomName;

            if (e.MessageServerType == "niwavided")
            {
                if (IsWatchWithTimeshift)
                {
                    string waybackKey = await NicoLiveProvider.GetWaybackKeyAsync(e.ThreadId);

                    _NicoLiveCommentClient = new Niwavided.NiwavidedNicoTimeshiftCommentClient(
                        e.MessageServerUrl,
                        e.ThreadId,
                        NiconicoSession.UserIdString,
                        waybackKey,
                        LiveInfo.VideoInfo.Video.OpenTime.Value
                        );
                }
                else
                {
                    _NicoLiveCommentClient = new Niwavided.NiwavidedNicoLiveCommentClient(
                        e.MessageServerUrl,
                        e.ThreadId,
                        NiconicoSession.UserIdString,
                        NiconicoSession.Context.HttpClient
                        );
                }

                _NicoLiveCommentClient.Connected       += _NicoLiveCommentClient_Connected;
                _NicoLiveCommentClient.Disconnected    += _NicoLiveCommentClient_Disconnected;
                _NicoLiveCommentClient.CommentRecieved += _NicoLiveCommentClient_CommentRecieved;
                _NicoLiveCommentClient.CommentPosted   += _NicoLiveCommentClient_CommentPosted;

                // コメントの受信処理と映像のオープンが被ると
                // 自動再生が失敗する?ので回避のため数秒遅らせる
                // タイムシフトコメントはStartTimeへのシーク後に取得されることを想定して
                if (!(_NicoLiveCommentClient is Niwavided.NiwavidedNicoTimeshiftCommentClient))
                {
                    _NicoLiveCommentClient.Open();

                    await Task.Delay(Services.Helpers.DeviceTypeHelper.IsMobile? 3000 : 500);
                }
            }
        }
Exemplo n.º 3
0
        private async void MessageWebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            string recievedText = string.Empty;

            using (var releaser = await _WebSocketLock.LockAsync())
            {
                using (var reader = new StreamReader(args.GetDataStream().AsStreamForRead()))
                {
                    recievedText = reader.ReadToEnd();
                    Debug.WriteLine($"<WebSocket Message> {args.MessageType}: {recievedText}");
                }
            }

            var param = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(recievedText);

            var type = (string)param["type"];

            if (type == "watch")
            {
                var body    = (JObject)param["body"];
                var command = (string)body["command"];
                switch (command)
                {
                case "servertime":

                    var paramItems       = (JArray)body["params"];
                    var serverTimeString = paramItems[0].ToString();
                    var serverTimeTick   = long.Parse(serverTimeString);
                    var serverTime       = DateTime.FromBinary(serverTimeTick);
                    RecieveServerTime?.Invoke(serverTime);
                    break;

                case "permit":
                    var permit = (string)(((JArray)body["params"])[0]);
                    RecievePermit?.Invoke(permit);
                    break;

                case "currentstream":

                    var current_stream    = (JObject)body["currentStream"];
                    var currentStreamArgs = new Live2CurrentStreamEventArgs()
                    {
                        Uri             = (string)current_stream["uri"],
                        Quality         = (string)current_stream["quality"],
                        QualityTypes    = ((JArray)current_stream["qualityTypes"]).Select(x => x.ToString()).ToArray(),
                        MediaServerType = (string)current_stream["mediaServerType"],
                        //MediaServerAuth = (string)current_stream["mediaServerAuth"],
                        StreamingProtocol = (string)current_stream["streamingProtocol"]
                    };
                    if (current_stream.TryGetValue("name", out var name))
                    {
                        currentStreamArgs.Name = (string)name;
                    }
                    RecieveCurrentStream?.Invoke(currentStreamArgs);
                    break;

                case "currentroom":
                    var room            = (JObject)body["room"];
                    var currentRoomArgs = new Live2CurrentRoomEventArgs()
                    {
                        MessageServerUrl  = (string)room["messageServerUri"],
                        MessageServerType = (string)room["messageServerType"],
                        RoomName          = (string)room["roomName"],
                        ThreadId          = (string)room["threadId"],
                        Forks             = ((JArray)room["forks"]).Select(x => (int)x).ToArray(),
                        ImportedForks     = ((JArray)room["importedForks"]).Select(x => (int)x).ToArray()
                    };
                    RecieveCurrentRoom?.Invoke(currentRoomArgs);
                    break;

                case "statistics":
                    var countItems = ((JArray)body["params"])
                                     .Select(x => x.ToString())
                                     .Select(x => long.TryParse(x, out var result) ? result : 0)
                                     .ToArray();
                    var statisticsArgs = new Live2StatisticsEventArgs()
                    {
                        ViewCount    = countItems[0],
                        CommentCount = countItems[1],
                        Count_3      = countItems[2],
                        Count_4      = countItems[3],
                    };
                    RecieveStatistics?.Invoke(statisticsArgs);
                    break;

                case "watchinginterval":
                    var timeString = ((JArray)body["params"]).Select(x => x.ToString()).ToArray()[0];
                    var time       = TimeSpan.FromSeconds(long.Parse(timeString));
                    RecieveWatchInterval?.Invoke(time);

                    WatchingHeartbaetTimer = new Timer((state) =>
                    {
                        SendMessageAsync($"{{\"type\":\"watch\",\"body\":{{\"command\":\"watching\",\"params\":[\"{Props.Program.BroadcastId}\",\"-1\",\"0\"]}}}}").ConfigureAwait(false);
                    }
                                                       , null, time, time
                                                       );
                    break;

                case "schedule":
                    var updateParam  = (JObject)body["update"];
                    var scheduleArgs = new Live2ScheduleEventArgs()
                    {
                        BeginTime = DateTime.FromBinary((long)updateParam["begintime"]),
                        EndTime   = DateTime.FromBinary((long)updateParam["endtime"]),
                    };
                    RecieveSchedule?.Invoke(scheduleArgs);
                    break;

                case "disconnect":
                    var disconnectParams = ((JArray)body["params"]).Select(x => x.ToString()).ToArray();
                    var endtimeString    = disconnectParams[0];
                    var endTime          = DateTime.FromBinary(long.Parse(endtimeString));
                    var endReason        = disconnectParams[1];
                    RecieveDisconnect?.Invoke();
                    break;

                case "postkey":
                    var postkeyParams = ((JArray)body["params"]).Select(x => x.ToString()).ToArray();
                    var postKey       = postkeyParams[0];

                    _Postkey = postKey;
                    break;
                }
            }
            else if (type == "ping")
            {
                await SendMessageAsync("{\"type\":\"pong\",\"body\":{}}");
            }
        }
Exemplo n.º 4
0
        private async void MessageWebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            string recievedText = string.Empty;

            using (var releaser = await _WebSocketLock.LockAsync())
            {
                using (var reader = new StreamReader(args.GetDataStream().AsStreamForRead()))
                {
                    recievedText = reader.ReadToEnd();
                    Debug.WriteLine($"{args.MessageType}: {recievedText}");
                }
            }

            var param = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(recievedText);

            var type = (string)param["type"];

            if (type == "watch")
            {
                var body    = (JObject)param["body"];
                var command = (string)body["command"];
                switch (command)
                {
                case "servertime":

                    var paramItems       = (JArray)body["params"];
                    var serverTimeString = paramItems[0].ToString();
                    var serverTimeTick   = long.Parse(serverTimeString);
                    var serverTime       = DateTime.FromBinary(serverTimeTick);
                    RecieveServerTime?.Invoke(serverTime);
                    break;

                case "permit":
                    var permit = (string)(((JArray)body["params"])[0]);
                    RecievePermit?.Invoke(permit);
                    break;

                case "currentstream":

                    var current_stream    = (JObject)body["currentStream"];
                    var currentStreamArgs = new Live2CurrentStreamEventArgs()
                    {
                        Uri               = (string)current_stream["uri"],
                        Name              = (string)current_stream["name"],
                        Quality           = (string)current_stream["quality"],
                        QualityTypes      = ((JArray)current_stream["qualityTypes"]).Select(x => x.ToString()).ToArray(),
                        MediaServerType   = (string)current_stream["mediaServerType"],
                        MediaServerAuth   = (string)current_stream["mediaServerAuth"],
                        StreamingProtocol = (string)current_stream["streamingProtocol"]
                    };
                    RecieveCurrentStream?.Invoke(currentStreamArgs);
                    break;

                case "currentroom":
                    var room            = (JObject)body["room"];
                    var currentRoomArgs = new Live2CurrentRoomEventArgs()
                    {
                        MessageServerUrl  = (string)room["messageServerUri"],
                        MessageServerType = (string)room["messageServerType"],
                        RoomName          = (string)room["roomName"],
                        ThreadId          = (string)room["threadId"],
                        Forks             = ((JArray)room["forks"]).Select(x => (int)x).ToArray(),
                        ImportedForks     = ((JArray)room["importedForks"]).Select(x => (int)x).ToArray()
                    };
                    RecieveCurrentRoom?.Invoke(currentRoomArgs);
                    break;

                case "statistics":
                    var countItems     = ((JArray)body["params"]).Select(x => x.ToString()).ToArray();
                    var statisticsArgs = new Live2StatisticsEventArgs()
                    {
                        ViewCount    = int.Parse(countItems[0]),
                        CommentCount = int.Parse(countItems[1]),
                        Count_3      = int.Parse(countItems[2]),
                        Count_4      = int.Parse(countItems[3]),
                    };
                    RecieveStatistics?.Invoke(statisticsArgs);
                    break;

                case "watchinginterval":
                    var timeString = ((JArray)body["params"]).Select(x => x.ToString()).ToArray()[0];
                    var time       = TimeSpan.FromSeconds(int.Parse(timeString));
                    RecieveWatchInterval?.Invoke(time);
                    break;

                case "schedule":
                    var updateParam  = (JObject)body["update"];
                    var scheduleArgs = new Live2ScheduleEventArgs()
                    {
                        BeginTime = DateTime.FromBinary((long)updateParam["begintime"]),
                        EndTime   = DateTime.FromBinary((long)updateParam["endtime"]),
                    };
                    RecieveSchedule?.Invoke(scheduleArgs);
                    break;

                case "disconnect":
                    var disconnectParams = ((JArray)body["params"]).Select(x => x.ToString()).ToArray();
                    var endtimeString    = disconnectParams[0];
                    var endTime          = TimeSpan.FromSeconds(int.Parse(endtimeString));
                    var endReason        = disconnectParams[1];
                    RecieveDisconnect?.Invoke();
                    break;
                }
            }
            else if (type == "ping")
            {
            }
        }