Пример #1
0
        public void SendDirectMessage(string input)
        {
            try
            {
                var s       = input.Split(" ");
                var message = "";
                for (int i = 2; i < s.Length; i++)
                {
                    message += s[i] + " ";
                }
                message = message.TrimEnd(' ');
                // Prepare chat message
                var directMessage = new DirectChatMessage
                {
                    Content   = message,
                    SessionId = SessionId,
                    ToUserId  = Convert.ToInt32(s[1])
                };

                // Send message
                SendMessage(JsonSerializer.Serialize(directMessage));
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
        }
 public void OnDirectChatMessage(DirectChatMessage message)
 {
     writeLine("-+-match direct chat " + message.Message + " from " + message.UserId);
 }
Пример #3
0
 public void onDirectChatMessage(DirectChatMessage directMessage)
 {
     addToConsole("---Direct Chat Message: " + directMessage.message + " from: " + directMessage.userId);
 }
Пример #4
0
 public void RemoveMessages(DirectChatMessage message)
 {
     savedMessages.Remove(message);
 }
Пример #5
0
 public void SaveMessage(DirectChatMessage message)
 {
     savedMessages.Add(message);
 }
Пример #6
0
    private void OnMessageReceived(object sender, MessageEventArgs e)
    {
        string message = e.Data;

        UnityThreadHelper.Dispatcher.Dispatch(() => {
            if (message.Equals(""))
            {
                Debug.Log("--empty web socket message");
                return;
            }
            if (message.Length == 1)
            {
                Debug.Log("--received empty message");
                return;
            }
            if (message.Length == 2)
            {
                Debug.Log("--received server dc message");
            }

            String[] splitText = message.Replace("\n\n", "\r").Split("\r".ToCharArray());
            String[] headers   = splitText[0].Split("\n".ToCharArray());

            if (headers[0].Equals("CONNECTED"))
            {
                for (int i = 0; i < headers.Length; i++)
                {
                    if (headers[i].StartsWith("heart-beat"))
                    {
                        String heartBeat = headers[i].Split(":".ToCharArray())[1];
                        stompWebsocketHandler.heartBeatPeriod = Int32.Parse(heartBeat.Split(",".ToCharArray())[1]);
                        if (stompWebsocketHandler.heartBeatPeriod != 0)
                        {
                            stompWebsocketHandler.CheckAlive();
                        }
                        Debug.Log("realtime heart: " + stompWebsocketHandler.heartBeatPeriod);
                    }
                    else if (headers[i].StartsWith("user-id"))
                    {
                        String userId = headers[i].Split(":".ToCharArray())[1];
                        OnConnected(userId);
                    }
                }
                return;
            }
            else if (headers.Length == 3 && headers[0].Equals("ERROR"))
            {
                Debug.Log("--ERROR: " + headers[1] + " " + headers[2]);
                return;
            }
            else if (splitText.Length == 1)
            {
                Debug.Log("--unknown at realtime: " + message);
                return;
            }
            String body    = splitText[1];
            String command = headers[0];
            if (command.Equals("MESSAGE"))
            {
                body = body.Substring(0, body.Length - 1);
                BacktoryRealtimeMessage backtoryRealtimeMessage = (BacktoryRealtimeMessage)
                                                                  fastJSON.JSON.ToObject(body, typeof(BacktoryRealtimeMessage));
                String _class = backtoryRealtimeMessage._type;
                if (_class == null)
                {
                    Debug.Log(message);
                    OnMessage(message);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.EXCEPTION))
                {
                    Debug.Log(BacktoryConnectivityMessage.EXCEPTION);
                    OnException(body);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.DIRECT))
                {
                    Debug.Log(BacktoryRealtimeMessage.DIRECT);
                    DirectChatMessage directMessage = (DirectChatMessage)
                                                      fastJSON.JSON.ToObject(body, typeof(DirectChatMessage));
                    OnDirectChatMessage(directMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHALLENGE))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHALLENGE);
                    ChallengeEventMessage challengeMessage = (ChallengeEventMessage)
                                                             fastJSON.JSON.ToObject(body, typeof(ChallengeEventMessage));
                    OnChallengeEventMessage(challengeMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHAT))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHAT);
                    ChallengeChatMessage chatMessage = (ChallengeChatMessage)
                                                       fastJSON.JSON.ToObject(body, typeof(ChallengeChatMessage));
                    OnChallengeChatMessage(chatMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHALLENGE_JOINED))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHALLENGE_JOINED);
                    ChallengeJoinedMessage challengeMessage = (ChallengeJoinedMessage)
                                                              fastJSON.JSON.ToObject(body, typeof(ChallengeJoinedMessage));
                    OnChallengeJoined(challengeMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.RESPONSE_RECEIVED))
                {
                    Debug.Log(BacktoryRealtimeMessage.RESPONSE_RECEIVED);
                    BacktoryRealtimeMessage responseMessage = (BacktoryRealtimeMessage)
                                                              fastJSON.JSON.ToObject(body, typeof(BacktoryRealtimeMessage));
                    OnResultReceived(responseMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.MASTER))
                {
                    Debug.Log(BacktoryRealtimeMessage.MASTER);
                    MasterMessage masterMessage = (MasterMessage)
                                                  fastJSON.JSON.ToObject(body, typeof(MasterMessage));
                    OnMasterMessage(masterMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.JOINED_WEBHOOK))
                {
                    Debug.Log(BacktoryRealtimeMessage.JOINED_WEBHOOK);
                    JoinedWebhookMessage joindMessage = (JoinedWebhookMessage)
                                                        fastJSON.JSON.ToObject(body, typeof(JoinedWebhookMessage));
                    OnJoinedWebhookMessage(joindMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHALLENGE_ENDED))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHALLENGE_ENDED);
                    ChallengeEndedMessage challengeMessage = (ChallengeEndedMessage)
                                                             fastJSON.JSON.ToObject(body, typeof(ChallengeEndedMessage));
                    OnChallengeEndedMessage(challengeMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHALLENGE_DISCONNECT))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHALLENGE_DISCONNECT);
                    ChallengeDisconnectMessage challengeMessage = (ChallengeDisconnectMessage)
                                                                  fastJSON.JSON.ToObject(body, typeof(ChallengeDisconnectMessage));
                    OnChallengeDisconnectMessage(challengeMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.WEBHOOK_ERROR))
                {
                    Debug.Log(BacktoryRealtimeMessage.WEBHOOK_ERROR);
                    WebhookErrorMessage webhookMessage = (WebhookErrorMessage)
                                                         fastJSON.JSON.ToObject(body, typeof(WebhookErrorMessage));
                    OnWebhookErrorMessage(webhookMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.CHALLENGE_STARTED))
                {
                    Debug.Log(BacktoryRealtimeMessage.CHALLENGE_STARTED);
                    ChallengeStartedMessage challengeMessage = (ChallengeStartedMessage)
                                                               fastJSON.JSON.ToObject(body, typeof(ChallengeStartedMessage));
                    OnChallengeStarted(challengeMessage);
                }
                else if (_class.Equals(BacktoryRealtimeMessage.STARTED_WEBHOOK))
                {
                    Debug.Log(BacktoryRealtimeMessage.STARTED_WEBHOOK);
                    StartedWebhookMessage startedMessage = (StartedWebhookMessage)
                                                           fastJSON.JSON.ToObject(body, typeof(StartedWebhookMessage));
                    OnStartedWebhookMessage(startedMessage);
                }
                else
                {
                    Debug.Log("--received message: " + body);
                    OnMessage(body);
                }
            }
            else
            {
                Debug.Log("--received message: " + body);
            }
        });
    }