Ejemplo n.º 1
0
        private void OnWebSocketMessage(object sender, MessageEventArgs e)
        {
            if (!e.IsText || e.Data == "")
            {
                return;
            }

            if (!e.Data.Contains(PostmanMassageData.ProtocolMessageTag))
            {
                return;
            }

            string msgstr = e.Data.Split(new string[] { PostmanMassageData.ProtocolMessageTag }, StringSplitOptions.None)[1];

            if (msgstr == PostmanMassageData.PingReturnString)
            {
                invokePing = true;
            }
            else
            {
                try
                {
                    PublishMessageData msg = JsonConvert.DeserializeObject <PublishMessageData>(msgstr);

                    lock (((ICollection)messageStack).SyncRoot)
                        messageStack.Add(msg);
                }
                catch
                {
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        private IEnumerator PublishCoroutine(string channel, string message, string tag, string extention)
        {
            PublishMessageData pub  = new PublishMessageData(channel, message, tag, extention);
            string             json = JsonConvert.SerializeObject(pub);

            if (Application.platform == RuntimePlatform.Android)
            {
                webSocket.Send(PostmanMassageData.BuildMessage(MessageType.PUBLISH, json));
            }
            else
            {
                webSocket.SendAsync(PostmanMassageData.BuildMessage(MessageType.PUBLISH, json), null);
            }

            yield return(null);
        }
Ejemplo n.º 3
0
        void Update()
        {
            if (tryReconnect)
            {
                reconnecting = true;
                StartCoroutine(ReconnectCoroutine());
                tryReconnect = false;
            }

            if (invokeOnConnect)
            {
                if (OnConnect != null)
                {
                    OnConnect();
                }

                invokeOnConnect = false;
            }

            if (messageStack != null && messageStack.Count > 0)
            {
                PublishMessageData[] copyStack;

                lock (((ICollection)messageStack).SyncRoot)
                {
                    copyStack    = messageStack.ToArray();
                    messageStack = new List <PublishMessageData>();
                }

                foreach (PublishMessageData msg in copyStack)
                {
                    Debug.Log(string.Format("PostmanClient :: [{0}] > {1}", msg.channel, msg.message));

                    if (OnMessage != null)
                    {
                        OnMessage(msg);
                    }

                    latestMessage = msg;
                }
            }

            if (invokeOnClose)
            {
                if (OnClose != null)
                {
                    OnClose();
                }

                invokeOnClose = false;
            }

            if (invokePing)
            {
                Debug.Log("PostmanClient :: pong");

                if (OnPingPong != null)
                {
                    OnPingPong();
                }

                invokePing = false;
            }
        }