Exemplo n.º 1
0
        private void WsServer_OnMessage(string cid, WebSocket.Model.WSProtocal msg)
        {
            if (msg != null)
            {
                if (_dic1.ContainsKey(cid) && msg.Content != null && msg.Content.Any())
                {
                    var name = Encoding.UTF8.GetString(msg.Content);

                    if (string.IsNullOrEmpty(name))
                    {
                        return;
                    }

                    Task.Factory.StartNew(() =>
                    {
                        while (_dic1.ContainsKey(cid))
                        {
                            try
                            {
                                var data = SerializeHelper.Serialize(ServerInfoDataHelper.GetInfo(name));

                                _wsServer.Reply(cid, new WSProtocal(WSProtocalType.Text, Encoding.UTF8.GetBytes(data)));
                            }
                            catch
                            {
                                _dic1.TryRemove(cid, out DateTime v);
                                break;
                            }
                            ThreadHelper.Sleep(1000);
                        }
                    });
                }
                else
                {
                    _wsServer.Disconnect(cid);
                }
            }
        }