Beispiel #1
0
 public void onChatGroupOwnerAddedMessage(ChatGroupOwnerAddedMessage groupOwnerAddedMessage)
 {
     addToConsole("Chat group owner added message: " + groupOwnerAddedMessage.groupId);
 }
Beispiel #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. maybe ping response");
                return;
            }
            else if (e.RawData.Length == 1)
            {
                stompWebsocketHandler.heartBeaten();
            }
            else if (e.RawData.Length == 2)
            {
                Debug.Log("--received server dc message");
                return;
            }

            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("connectivity 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 main: " + message);
                return;
            }
            String body    = splitText[1];
            String command = headers[0];
            if (command.Equals("MESSAGE"))
            {
                foreach (String header in headers)
                {
                    String[] splitedHeader = header.Split(":".ToCharArray());
                    if (splitedHeader[0].Equals("delivery-id"))
                    {
                        stompWebsocketHandler.stomp.webSocket.Send("ACK\n" +
                                                                   "X-Backtory-Connectivity-Id:" + connectivityId + "\n" +
                                                                   "delivery-id:" + splitedHeader[1] + "\n" +
                                                                   "\n\0");
                    }
                }

                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(BacktoryConnectivityMessage.CHAT_GROUP_CREATED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_CREATED_MESSAGE);
                    ChatGroupCreatedMessage chatMessage = (ChatGroupCreatedMessage)
                                                          fastJSON.JSON.ToObject(body, typeof(ChatGroupCreatedMessage));
                    OnChatGroupCreatedMessage(chatMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_MEMBERS_LIST_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_MEMBERS_LIST_MESSAGE);
                    ChatGroupMembersListMessage listMessage = (ChatGroupMembersListMessage)
                                                              fastJSON.JSON.ToObject(body, typeof(ChatGroupMembersListMessage));
                    OnChatGroupMembersListMessage(listMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUPS_LIST_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUPS_LIST_MESSAGE);
                    ChatGroupsListMessage chatMessage = (ChatGroupsListMessage)
                                                        fastJSON.JSON.ToObject(body, typeof(ChatGroupsListMessage));
                    OnChatGroupsListMessage(chatMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_OWNER_ADDED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_OWNER_ADDED_MESSAGE);
                    ChatGroupOwnerAddedMessage addedMessage = (ChatGroupOwnerAddedMessage)
                                                              fastJSON.JSON.ToObject(body, typeof(ChatGroupOwnerAddedMessage));
                    OnChatGroupOwnerAddedMessage(addedMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_LEFT_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_LEFT_MESSAGE);
                    ChatGroupLeftMessage leftMessage = (ChatGroupLeftMessage)
                                                       fastJSON.JSON.ToObject(body, typeof(ChatGroupLeftMessage));
                    OnChatGroupLeftMessage(leftMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_INVITED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_INVITED_MESSAGE);
                    ChatGroupInvitedMessage invitedMessage = (ChatGroupInvitedMessage)
                                                             fastJSON.JSON.ToObject(body, typeof(ChatGroupInvitedMessage));
                    OnChatGroupInvitedMessage(invitedMessage);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_USER_REMOVED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_USER_REMOVED_MESSAGE);
                    UserRemovedMessage removedMessage = (UserRemovedMessage)
                                                        fastJSON.JSON.ToObject(body, typeof(UserRemovedMessage));
                    OnChatGroupUserRemovedMessage(removedMessage);
                    sendDelivery(removedMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_USER_ADDED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_USER_ADDED_MESSAGE);
                    UserAddedMessage addedMessage = (UserAddedMessage)
                                                    fastJSON.JSON.ToObject(body, typeof(UserAddedMessage));
                    OnChatGroupUserAddedMessage(addedMessage);
                    sendDelivery(addedMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_USER_JOINED_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_USER_JOINED_MESSAGE);
                    UserJoinedMessage joinedMessage = (UserJoinedMessage)
                                                      fastJSON.JSON.ToObject(body, typeof(UserJoinedMessage));
                    OnChatGroupUserJoinedMessage(joinedMessage);
                    sendDelivery(joinedMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_USER_LEFT_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_USER_LEFT_MESSAGE);
                    UserLeftMessage leftMessage = (UserLeftMessage)
                                                  fastJSON.JSON.ToObject(body, typeof(UserLeftMessage));
                    OnChatGroupUserLeftMessage(leftMessage);
                    sendDelivery(leftMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.CHAT_GROUP_INVITATION_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.CHAT_GROUP_INVITATION_MESSAGE);
                    ChatInvitationMessage invitationMessage = (ChatInvitationMessage)
                                                              fastJSON.JSON.ToObject(body, typeof(ChatInvitationMessage));
                    OnChatInvitationMessage(invitationMessage);
                    sendDelivery(invitationMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.SIMPLE_CHAT_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.SIMPLE_CHAT_MESSAGE);
                    SimpleChatMessage simpleMessage = (SimpleChatMessage)
                                                      fastJSON.JSON.ToObject(body, typeof(SimpleChatMessage));
                    onSimpleMessage(simpleMessage);
                    sendDelivery(simpleMessage.deliveryId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.USER_CHAT_HISTORY_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.USER_CHAT_HISTORY_MESSAGE);
                    UserChatHistoryMessage historyMessage = (UserChatHistoryMessage)
                                                            fastJSON.JSON.ToObject(body, typeof(UserChatHistoryMessage));
                    List <ChatMessage> messageList = parseMessageList(historyMessage.messageList, false);
                    OnUserChatHistoryMessage(messageList);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.GROUP_CHAT_HISTORY_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.GROUP_CHAT_HISTORY_MESSAGE);
                    GroupChatHistoryMessage historyMessage = (GroupChatHistoryMessage)
                                                             fastJSON.JSON.ToObject(body, typeof(GroupChatHistoryMessage));
                    List <ChatMessage> messageList = parseMessageList(historyMessage.messageList, false);
                    OnGroupChatHistoryMessage(messageList, historyMessage.groupId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.OFFLINE_CHAT_MESSAGE_LIST))
                {
                    Debug.Log(BacktoryConnectivityMessage.OFFLINE_CHAT_MESSAGE_LIST);
                    OfflineChatMessageList offlineMessageList = (OfflineChatMessageList)
                                                                fastJSON.JSON.ToObject(body, typeof(OfflineChatMessageList));
                    // if (offlineMessageList.messageList.Count == 0) {
                    //     OnEmptyOffline();
                    //     return;
                    // }
                    List <ChatMessage> messageList = parseMessageList(offlineMessageList.messageList, true);
                    OnOfflineChatMessage(messageList);
                }

                else if (_class.Equals(BacktoryConnectivityMessage.EXCEPTION))
                {
                    Debug.Log(BacktoryConnectivityMessage.EXCEPTION);
                    OnException(body);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.MATCHMAKING_CANCELLATION_RESPONSE))
                {
                    Debug.Log(BacktoryConnectivityMessage.MATCHMAKING_CANCELLATION_RESPONSE);
                    MatchmakingCancellationResponseMessage onMatchCancellationResponse = (MatchmakingCancellationResponseMessage)
                                                                                         fastJSON.JSON.ToObject(body, typeof(MatchmakingCancellationResponseMessage));
                    requestList.Remove(onMatchCancellationResponse.requestId);
                    OnMatchCancellationResponse(onMatchCancellationResponse.requestId);
                }
                else if (_class.Equals(BacktoryConnectivityMessage.MATCHMAKING_RESPONSE))
                {
                    Debug.Log(BacktoryConnectivityMessage.MATCHMAKING_RESPONSE);
                    MatchmakingResponseMessage matchMakingResponse = (MatchmakingResponseMessage)
                                                                     fastJSON.JSON.ToObject(body, typeof(MatchmakingResponseMessage));
                    requestList.Add(matchMakingResponse.requestId);
                    if (OnMatchResponse != null)
                    {
                        OnMatchResponse(matchMakingResponse.requestId);
                    }
                    else
                    {
                        Debug.Log("ghalat ast");
                    }
                }
                else if (_class.Equals(BacktoryConnectivityMessage.MATCH_FOUND_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.MATCH_FOUND_MESSAGE);
                    MatchFoundMessage matchFoundMessage = (MatchFoundMessage)
                                                          fastJSON.JSON.ToObject(body, typeof(MatchFoundMessage));
                    if (requestList.Contains(matchFoundMessage.requestId))
                    {
                        OnMatchFound(matchFoundMessage);
                    }
                    else
                    {
                        Debug.Log("--Matchmaking for another device of user");
                    }
                }
                else if (_class.Equals(BacktoryConnectivityMessage.MATCH_NOT_FOUND_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.MATCH_NOT_FOUND_MESSAGE);
                    MatchNotFoundMessage matchNotFoundMessage = (MatchNotFoundMessage)
                                                                fastJSON.JSON.ToObject(body, typeof(MatchNotFoundMessage));
                    if (requestList.Contains(matchNotFoundMessage.requestId))
                    {
                        OnMatchNotFound(matchNotFoundMessage);
                    }
                    else
                    {
                        Debug.Log("--Matchmaking not found for another device of user");
                    }
                }
                else if (_class.Equals(BacktoryConnectivityMessage.MATCH_UPDATE_MESSAGE))
                {
                    Debug.Log(BacktoryConnectivityMessage.MATCH_UPDATE_MESSAGE);
                    MatchUpdateMessage matchUpdateMessage = (MatchUpdateMessage)
                                                            fastJSON.JSON.ToObject(body, typeof(MatchUpdateMessage));
                    OnMatchUpdate(matchUpdateMessage);
                }
                else
                {
                    OnMessage(body);
                }
            }
            else
            {
                Debug.Log("--received message: " + body);
            }
        });
    }