Exemplo n.º 1
0
 private static IPromise InitializeWebSocket()
 {
     _client          = new WebSocket("wss://connect-gateway.unity.com/v1");
     _client.OnClose += (sender, e) =>
     {
         UnityMainThreadDispatcher.Instance().Enqueue(() =>
         {
             reconnecting = true;
             UpdateWindowCanvas();
             InitializeWebSocket().Then(SendConnectFrame);
         });
     };
     _client.OnMessage += (sender, args) =>
     {
         var frame = Frame.FromJson(JsonValue.Parse(args.Data));
         var type  = frame.GetType();
         if (Processors.ContainsKey(type))
         {
             Processors[type](frame);
             UnityMainThreadDispatcher.Instance().Enqueue(UpdateWindowCanvas);
         }
     };
     return(_client.AsyncConnect());
 }
Exemplo n.º 2
0
        private static void ProcessMessageCreateFrame(MessageCreateFrame messageCreateFrame)
        {
            var frameMessage = messageCreateFrame.data;

            if (frameMessage.type == "normal")
            {
                Users[frameMessage.author.id] = frameMessage.author;
            }

            if (Messages.ContainsKey(frameMessage.channelId))
            {
                var idx = -1;
                for (var i = 0; i < Messages[frameMessage.channelId].Count; ++i)
                {
                    if (Messages[frameMessage.channelId][i].nonce == frameMessage.nonce)
                    {
                        idx = i;
                        break;
                    }
                }

                if (idx > -1)
                {
                    Messages[frameMessage.channelId][idx] = frameMessage;
                }
                else
                {
                    var addLately = false;
                    if (_instance != null)
                    {
                        using (_instance.window.getScope())
                        {
                            addLately = HomePage.currentState.SelectedChannelId == frameMessage.channelId &&
                                        ChattingWindow.currentState.m_ScrollController.offset > 50f;
                        }
                    }

                    if (addLately)
                    {
                        NewMessages.Add(frameMessage);
                    }
                    else
                    {
                        Messages[frameMessage.channelId].Insert(0, frameMessage);
                    }
                }
            }

            string selectedChannelId = null;

            if (_instance != null)
            {
                using (_instance.window.getScope())
                {
                    if (HomePage.currentState != null)
                    {
                        selectedChannelId = HomePage.currentState.SelectedChannelId;
                    }
                }
            }

            if (selectedChannelId != null)
            {
                if (frameMessage.channelId == selectedChannelId &&
                    ReadStates.ContainsKey(selectedChannelId))
                {
                    ReadStates[selectedChannelId].lastMessageId = frameMessage.id;
                }
            }

            if (frameMessage.mentionEveryone ||
                frameMessage.mentions.Any(m => m.id == currentUserId))
            {
                ReadStates[frameMessage.channelId].lastMentionId = frameMessage.id;
                if (selectedChannelId != null)
                {
                    if (frameMessage.channelId != selectedChannelId)
                    {
                        ReadStates[frameMessage.channelId].mentionCount += 1;
                    }
                }
            }

            if (Channels.ContainsKey(messageCreateFrame.data.channelId))
            {
                Channels[messageCreateFrame.data.channelId].lastMessage =
                    messageCreateFrame.data;
            }

            if (Channels.ContainsKey(frameMessage.channelId) &&
                !Channels[frameMessage.channelId].isMute &&
                frameMessage.author.id != currentUserId)
            {
                var userId              = messageCreateFrame.data.author.id;
                var username            = messageCreateFrame.data.author.fullName;
                var avatar              = messageCreateFrame.data.author.avatar;
                var content             = messageCreateFrame.data.content;
                var msgId               = messageCreateFrame.data.id;
                var channelId           = messageCreateFrame.data.channelId;
                var inChannel           = messageCreateFrame.data.channelId == selectedChannelId;
                var notificationContent = "";

                if (messageCreateFrame.data.type == "voice")
                {
                    notificationContent = $"{username}: [语音]";
                }
                else if (messageCreateFrame.data.attachments != null && messageCreateFrame.data.attachments.Count > 0)
                {
                    var contentType = messageCreateFrame.data.attachments.first().contentType;
                    if (contentType == "image/png" || contentType == "image/jpg" || contentType == "image/jpeg" ||
                        contentType == "image/gif")
                    {
                        notificationContent = $"{username}: [图片]";
                    }
                    else
                    {
                        notificationContent = $"{username}: [文件]";
                    }
                }
                else
                {
                    notificationContent = $"{username}: {ParseMessageToString(content, Users)}";
                }

                UnityMainThreadDispatcher.Instance().Enqueue(() => NotificationManager.instance.ShowNotification(
                                                                 inChannel,
                                                                 userId,
                                                                 $"{Channels[channelId].name}",
                                                                 notificationContent,
                                                                 msgId,
                                                                 channelId,
                                                                 avatar,
                                                                 id =>
                {
                    var instance = _instance == null
                            ? GetWindow <Window>("Messenger", typeof(SceneView))
                            : _instance;

                    using (instance.window.getScope())
                    {
                        HomePage.currentState.Select(id);
                    }
                }));
            }
        }
 public static UnityMainThreadDispatcher Instance()
 {
     return(_instance ?? (_instance = new UnityMainThreadDispatcher()));
 }