Exemplo n.º 1
0
        /// <summary>
        /// 消息处理
        /// </summary>
        /// <returns></returns>
        static async Task EchoLoop(string userId, CancellationToken ct)
        {
            List <NoticetemplateModule> dummy;

            System.Net.WebSockets.WebSocket socketdummy;
            //处理消息
            if (_socketMessages.ContainsKey(userId))
            {
                List <NoticetemplateModule> msgs = _socketMessages[userId];
                foreach (NoticetemplateModule item in msgs)
                {
                    await SendStringAsync(socket, JsonConvert.SerializeObject(item));

                    _socketMessages.TryRemove(userId, out dummy);
                }
            }

            //循环监听
            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                //处理数据
                string response = await ReceiveStringAsync(socket, ct);

                NoticetemplateModule msg = JsonConvert.DeserializeObject <NoticetemplateModule>(response);
                msg.CreateTime = DateTime.Now;

                //是否有这个用户的消息池
                if (!_socketMessages.ContainsKey(userId))
                {
                    _socketMessages.TryAdd(userId, new List <NoticetemplateModule>());
                }
                //消息池加数据
                _socketMessages[userId].Add(msg);



                //不停地去跑循环
                foreach (var socket in _sockets)
                {
                    //移除
                    if (socket.Value.State != WebSocketState.Open)
                    {
                        if (_sockets.ContainsKey(userId))
                        {
                            _sockets.TryRemove(userId, out socketdummy);
                        }
                        break;
                    }
                    if (socket.Key == msg.RecriverId || socket.Key == userId)
                    {
                        await SendStringAsync(socket.Value, JsonConvert.SerializeObject(msg), ct);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static async Task Acceptor(HttpContext context, Func <Task> n)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                return;
            }


            CancellationToken ct = context.RequestAborted;

            socket = await context.WebSockets.AcceptWebSocketAsync();

            TokenModelBeta tokenModel = JwtToken.SerializeJWT(context.Request.Query["Authorization"].ToString());

            //处理用户连接
            //添加Socket进集合 存在就取出 不存在就加入
            if (!_sockets.ContainsKey(tokenModel.Id))
            {
                //用户第一次连接上来 给一个欢迎消息
                _sockets.TryAdd(tokenModel.Id, socket);
                NoticetemplateModule msg = new NoticetemplateModule();
                msg.id              = "0";
                msg.title           = "欢迎使用";
                msg.read            = false;
                msg.status          = "doing";
                msg.type            = "通知";
                msg.description     = "已成功连接WebSocket";
                msg.avatar          = "";
                msg.creatorUserName = "******";
                msg.datetime        = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                msg.RecriverId      = tokenModel.Id;
                await SendStringAsync(socket, JsonConvert.SerializeObject(msg));
            }
            else
            {
                if (_sockets[tokenModel.Id] != socket)
                {
                    _sockets[tokenModel.Id] = socket;
                }
            }

            //处理消息
            await EchoLoop(tokenModel.Id, ct);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="userId">链接人</param>
        /// <param name="ct"></param>
        /// <returns></returns>
        static async Task EchoLoop(string userId, CancellationToken ct)
        {
            List <NoticetemplateModule> dummy;

            System.Net.WebSockets.WebSocket socketdummy;



            //用户一旦登录进来开始处理数据  处理消息
            if (_socketMessages.ContainsKey(userId))
            {
                List <NoticetemplateModule> msgs = _socketMessages[userId];
                foreach (NoticetemplateModule item in msgs)
                {
                    await SendStringAsync(socket, JsonConvert.SerializeObject(item));

                    _socketMessages.TryRemove(userId, out dummy);
                }
            }

            //循环监听
            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                //处理数据
                string response = await ReceiveStringAsync(socket, ct);

                NoticetemplateModule msg = JsonConvert.DeserializeObject <NoticetemplateModule>(response);
                msg.datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

                //是否有这个用户的消息池
                if (!_socketMessages.ContainsKey(msg.RecriverId))
                {
                    _socketMessages.TryAdd(msg.RecriverId, new List <NoticetemplateModule>());
                }
                //消息池加数据
                _socketMessages[msg.RecriverId].Add(msg);


                //不停地去跑循环
                foreach (var socket in _sockets)
                {
                    //移除
                    if (socket.Value.State != WebSocketState.Open)
                    {
                        if (_sockets.ContainsKey(userId))
                        {
                            _sockets.TryRemove(userId, out socketdummy);
                        }
                        continue;
                    }

                    //循环当前socket的人有没有数据需要接受
                    foreach (var item in _socketMessages[socket.Key].ToList())
                    {
                        //寻找当前链接人有没有消息
                        await SendStringAsync(socket.Value, JsonConvert.SerializeObject(item), ct);
                    }
                }
            }
        }