コード例 #1
0
 public void onChallengeJoined(ChallengeJoinedMessage joinedMessage)
 {
     addToConsole("---user joined: " + joinedMessage.userId + " name: " + joinedMessage.username);
 }
コード例 #2
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);
            }
        });
    }