private static void AddListSorted(List <IApiChannelMessage> messageList, IApiChannelMessage message)
 {
     messageList.Add(message);
     messageList.Sort((a, b) =>
     {
         var ordinal = string.CompareOrdinal(a.CreateTime, b.CreateTime);
         return(ordinal == 0 ? string.CompareOrdinal(a.MessageId, b.MessageId) : ordinal);
     });
 }
Esempio n. 2
0
        public void OnChannelMessage(object sender, IApiChannelMessage message)
        {
            var chatMessages = StateManager.Instance.ChatMessages;

            foreach (var topic in chatMessages.Keys)
            {
                if (topic.Equals(message.ChannelId))
                {
                    chatMessages[topic].Add(message.MessageId, message);
                }
            }
        }
        /// <summary>
        /// Adds new user message to channel ui
        /// </summary>
        private void AddMessage(IApiChannelMessage message)
        {
            //Declaring temp variables
            GameObject messagePrefab;

            //Checking if user who send message is local user
            if (message.SenderId == _connection.Account.User.Id)
            {
                messagePrefab = _thisUserMessagePrefab;
            }
            else
            {
                messagePrefab = _otherUserMessagePrefab;
            }

            // Instantiating message object as a child of _content
            GameObject messageGO = Instantiate(messagePrefab, _content) as GameObject;

            // Getting ChatMessageUI component from already instantiated message
            ChatMessageUI messageUI = messageGO.GetComponent <ChatMessageUI>();

            if (messageUI)
            {
                // If the message is from the same user as newest we should hide his username on this message
                bool hideUsername = (message.Username == _lastMessageUsername) && !message.Persistent;

                // Initializing message with given data
                messageUI.InitMessage(message.MessageId, message.Username, message.Content.FromJson <Dictionary <string, string> >()["content"], message.CreateTime, hideUsername);

                // Adding message to messages dict
                _messages.Add(message.MessageId, messageUI);

                // Setting last
                _lastMessageUsername = message.Username;

                //If message is historical change order in hierarchy, the latest historical message is the oldest
                if (message.Persistent)
                {
                    messageUI.transform.SetSiblingIndex(1);
                }
            }
            else
            {
                Debug.LogError("Invalid _thisUserMessagePrefab or _otherUserMessagePrefab! It should contains ChatMessageUI script.");
                Destroy(messageGO);
                return;
            }
        }
Esempio n. 4
0
 private void _socket_OnChannelMessage(object sender, IApiChannelMessage e)
 {
     Debug.Log("Call to socket_OnChannelMessage");
 }
Esempio n. 5
0
        /// <summary>
        /// Receives and translates incoming messages
        /// </summary>
        private void ReceiveMessage(IApiChannelMessage message, bool historical = false)
        {
            Debug.Log("Received message: " + message);

            ChatChannel channel;
            string      messageContent = "";

            //Search for chat channel which should receive message, if it's not created - creates it
            if (_chatChannels.ContainsKey(message.ChannelId))
            {
                channel = _chatChannels[message.ChannelId];
            }
            else
            {
                channel = CreateChannel(message.ChannelId);
            }

            //Translate message basing on message code
            switch (message.Code)
            {
            //Receiving chat message
            case 0:
                if (!string.IsNullOrEmpty(message.Content) && message.Content != "{}")
                {
                    messageContent = JsonParser.FromJson <Dictionary <string, string> >(message.Content)["message"];
                }

                channel.ChatMessage(message.MessageId, message.SenderId, message.Username, messageContent, message.CreateTime, historical);

                break;

            //Receiving chat message update
            case 1:
                if (!string.IsNullOrEmpty(message.Content) && message.Content != "{}")
                {
                    messageContent = JsonParser.FromJson <Dictionary <string, string> >(message.Content)["message"];
                }

                channel.ChatUpdate(message.MessageId, messageContent, message.CreateTime);

                break;

            //Receiving chat message remove
            case 2:
                channel.ChatRemove(message.MessageId);

                break;

            //Receiving information about user joined group
            case 3:
                channel.JoinedGroup(message.MessageId, message.Username, historical);

                break;

            //Receiving information about user added to group
            case 4:
                channel.AddedToGroup(message.MessageId, message.Username, historical);

                break;

            //Receiving information about user left group
            case 5:
                channel.LeftGroup(message.MessageId, message.Username, historical);

                break;

            //Receiving information about user kicked group
            case 6:
                channel.KickedFromGroup(message.MessageId, message.Username, historical);

                break;

            //Receiving information about user promoted in group
            case 7:
                channel.PromotedInGroup(message.MessageId, message.Username, historical);

                break;
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Dispatches <see cref="ReceiveMessage(IApiChannelMessage, bool)"/> to be runned in main thread
 /// </summary>
 private void ReceiveMessage(IApiChannelMessage message)
 {
     UnityMainThreadDispatcher.Instance().Enqueue(() => ReceiveMessage(message));
 }
        // -------------------------------------------

        /*
         * OnReceivedChannelMessage
         */
        private async void OnReceivedChannelMessage(IApiChannelMessage m)
        {
            Dictionary <string, object> message = (Dictionary <string, object>)Json.Deserialize(m.Content);

            foreach (KeyValuePair <string, object> item in message)
            {
                if (item.Key == ROOMS_CHAT_MESSAGE)
                {
                    string buf = (string)item.Value;
                    if (buf.Length > 0)
                    {
                        RoomsBuffer = (string)item.Value;
                    }
                    if (GameObject.FindObjectOfType <YourNetworkTools>() == null)
                    {
                        UIEventController.Instance.DispatchUIEvent(ClientTCPEventsController.EVENT_CLIENT_TCP_ESTABLISH_NETWORK_ID);
                    }
                    else
                    {
                        NetworkEventController.Instance.DelayLocalEvent(ClientTCPEventsController.EVENT_CLIENT_TCP_ESTABLISH_NETWORK_ID, 0.1f);
                    }
                    UIEventController.Instance.DelayUIEvent(ClientTCPEventsController.EVENT_CLIENT_TCP_LIST_OF_GAME_ROOMS, 0.3f);
                    if (DEBUG)
                    {
                        Debug.LogError("ROOMS_CHAT_MESSAGE::Message KEY=" + item.Key + "::ROOMS=" + RoomsBuffer);
                    }
                }
                else if (item.Key == REMOVE_ROOMS_MESSAGE)
                {
                    string   roomToDelete   = (string)item.Value;
                    string[] currentRooms   = RoomsBuffer.Split(ROOMS_SEPARATOR);
                    string   finalRooms     = "";
                    bool     hasBeenDeleted = false;
                    for (int i = 0; i < currentRooms.Length; i++)
                    {
                        if (currentRooms[i].IndexOf(roomToDelete) != -1)
                        {
                            if (DEBUG)
                            {
                                Debug.LogError("------------------DELETED ROOM=" + roomToDelete);
                            }
                            hasBeenDeleted = true;
                        }
                        else
                        {
                            finalRooms = currentRooms[i] + ((finalRooms.Length > 0) ? ROOMS_SEPARATOR + "" : "") + finalRooms;
                        }
                    }
                    if (hasBeenDeleted)
                    {
                        RoomsBuffer = finalRooms;
                        if (DEBUG)
                        {
                            Debug.LogError("******************ROOMS AFTER DELETE=" + finalRooms);
                        }
                        if (m_localUser != null)
                        {
                            if (NakamaConnection.Channel != null)
                            {
                                await NakamaConnection.LeaveMainChat();
                            }
                        }
                    }
                }
            }
        }
 private void ActionReceivedChannelMessage(IApiChannelMessage m)
 {
     m_mainThread.Enqueue(() => OnReceivedChannelMessage(m));
 }
Esempio n. 9
0
 void OnChannelMessage(object sender, IApiChannelMessage message)
 {
     Debug.Log("Message received");
     this.OnStatusChanged(this, string.Format("Message receive {0:dd.MM.yy hh:mm:ss}", System.DateTime.Now));
     this.OnMessageReceived(this, message);
 }