コード例 #1
0
        private async Task <bool> ConnectSocket()
        {
            try
            {
                this.socket.OnDisconnected -= Socket_OnDisconnected;
                await this.socket.Disconnect();

                JObject jobj = await this.GetJObjectAsync("socket/token?access_token=" + this.token.accessToken);

                if (jobj != null && jobj.ContainsKey("socket_token"))
                {
                    string socketToken = jobj["socket_token"].ToString();

                    this.socket.Listen("event", async(data) =>
                    {
                        try
                        {
                            if (data != null)
                            {
                                JObject eventJObj = JObject.Parse(data.ToString());
                                if (eventJObj.ContainsKey("type"))
                                {
                                    if (eventJObj["type"].Value <string>().Equals("donation", StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        var messages = eventJObj["message"] as JArray;
                                        if (messages != null)
                                        {
                                            foreach (var message in messages)
                                            {
                                                StreamlabsDonation slDonation = message.ToObject <StreamlabsDonation>();
                                                await EventService.ProcessDonationEvent(EventTypeEnum.StreamlabsDonation, slDonation.ToGenericDonation());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex);
                        }
                    });

                    this.socket.OnDisconnected += Socket_OnDisconnected;
                    await this.socket.Connect($"https://sockets.streamlabs.com?token={socketToken}");

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            return(false);
        }
コード例 #2
0
        protected override async Task <Result> InitializeInternal()
        {
            JObject jobj = await this.GetJObjectAsync("socket/token?access_token=" + this.token.accessToken);

            if (jobj != null && jobj.ContainsKey("socket_token"))
            {
                string socketToken = jobj["socket_token"].ToString();

                await this.socket.Connect($"https://sockets.streamlabs.com", $"token={socketToken}");

                this.socket.Listen("event", async(data) =>
                {
                    if (data != null)
                    {
                        JObject eventJObj = JObject.Parse(data.ToString());
                        if (eventJObj.ContainsKey("type"))
                        {
                            if (eventJObj["type"].Value <string>().Equals("donation", StringComparison.InvariantCultureIgnoreCase))
                            {
                                var messages = eventJObj["message"] as JArray;
                                if (messages != null)
                                {
                                    foreach (var message in messages)
                                    {
                                        StreamlabsDonation slDonation = message.ToObject <StreamlabsDonation>();
                                        await EventService.ProcessDonationEvent(EventTypeEnum.StreamlabsDonation, slDonation.ToGenericDonation());
                                    }
                                }
                            }
                        }
                    }
                });
                this.TrackServiceTelemetry("Streamlabs");
                return(new Result());
            }
            return(new Result(Resources.StreamlabsWebSocketTokenFailed));
        }