Exemplo n.º 1
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.º 2
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")
            {
            }
        }