Пример #1
0
        protected virtual async Task OnMessageEvent(object sender, WSMessageClientEventArgs args)
        {
            switch (args.MessageEventType)
            {
            case MessageEventType.Sent:
                break;

            case MessageEventType.Receive:
                if (args.Message.Trim().ToLower() == "ping" || args.Packet.Data.Trim().ToLower() == "ping")
                {
                    await _websocketClient.SendToServerRawAsync("pong");
                }
                else
                {
                    try
                    {
                        var          message      = JsonConvert.DeserializeObject <MessageBase>(args.Packet.Data);
                        IMessageBase messageTyped = null;

                        switch (message.ProviderType)
                        {
                        case ProviderType.Twitch:
                            messageTyped = JsonConvert.DeserializeObject <MessageTwitch>(args.Packet.Data);

                            if (MessageTwitchEvent != null)
                            {
                                MessageTwitchEvent?.Invoke(sender, messageTyped as IMessageTwitch);
                            }
                            break;

                        case ProviderType.Discord:
                            messageTyped = JsonConvert.DeserializeObject <MessageDiscord>(args.Packet.Data);

                            if (MessageDiscordEvent != null)
                            {
                                MessageDiscordEvent?.Invoke(sender, messageTyped as IMessageDiscord);
                            }
                            break;

                        case ProviderType.Tcp:
                            messageTyped = JsonConvert.DeserializeObject <MessageTcp>(args.Packet.Data);

                            if (MessageTcpEvent != null)
                            {
                                MessageTcpEvent?.Invoke(sender, messageTyped as IMessageTcp);
                            }
                            break;

                        case ProviderType.Websocket:
                            messageTyped = JsonConvert.DeserializeObject <MessageWS>(args.Packet.Data);

                            if (MessageWebsocketEvent != null)
                            {
                                MessageWebsocketEvent?.Invoke(sender, messageTyped as IMessageWS);
                            }
                            break;

                        default:
                            break;
                        }

                        if (messageTyped != null && MessageEvent != null)
                        {
                            MessageEvent?.Invoke(sender, messageTyped);
                        }
                    }
                    catch
                    {
                        if (SystemMessageEvent != null)
                        {
                            SystemMessageEvent?.Invoke(sender, args.Message);
                        }
                    }
                }
                break;

            default:
                break;
            }
        }
Пример #2
0
        protected virtual async Task OnMessageEvent(object sender, TcpMessageClientEventArgs args)
        {
            switch (args.MessageEventType)
            {
            case MessageEventType.Sent:
                break;

            case MessageEventType.Receive:
                try
                {
                    var          message      = JsonConvert.DeserializeObject <MessageBase>(args.Packet.Data);
                    IMessageBase messageTyped = null;

                    switch (message.ProviderType)
                    {
                    case ProviderType.Twitch:
                        messageTyped = JsonConvert.DeserializeObject <MessageTwitch>(args.Packet.Data);

                        if (MessageTwitchEvent != null)
                        {
                            await MessageTwitchEvent?.Invoke(sender, messageTyped as IMessageTwitch);
                        }
                        break;

                    case ProviderType.Discord:
                        messageTyped = JsonConvert.DeserializeObject <MessageDiscord>(args.Packet.Data);

                        if (MessageDiscordEvent != null)
                        {
                            await MessageDiscordEvent?.Invoke(sender, messageTyped as IMessageDiscord);
                        }
                        break;

                    case ProviderType.Tcp:
                        messageTyped = JsonConvert.DeserializeObject <MessageTcp>(args.Packet.Data);

                        if (MessageTcpEvent != null)
                        {
                            await MessageTcpEvent?.Invoke(sender, messageTyped as IMessageTcp);
                        }
                        break;

                    case ProviderType.Websocket:
                        messageTyped = JsonConvert.DeserializeObject <MessageWS>(args.Packet.Data);

                        if (MessageWebsocketEvent != null)
                        {
                            await MessageWebsocketEvent?.Invoke(sender, messageTyped as IMessageWS);
                        }
                        break;

                    default:
                        break;
                    }

                    if (messageTyped != null && MessageEvent != null)
                    {
                        await MessageEvent?.Invoke(sender, messageTyped);
                    }
                }
                catch
                {
                    if (SystemMessageEvent != null)
                    {
                        await SystemMessageEvent?.Invoke(sender, args.Message);
                    }
                }
                break;

            default:
                break;
            }
        }