示例#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;
            }
        }