Ejemplo n.º 1
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task ProcessChat(AspNetWebSocketContext context)
        {
            WebSocket   socket = context.WebSocket;
            ChatManager cm     = ChatManager.CreateInstance(_socketid, _imuid, socket, _sourcetype);

            while (socket.State == WebSocketState.Open)
            {
                ArraySegment <byte>    buffer = new ArraySegment <byte>(new byte[2048]);
                WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, cm.CancelToken);

                if (result.MessageType == WebSocketMessageType.Close || socket.State == WebSocketState.CloseReceived) //如果输入帧为取消帧,发送close命令
                {
                    cm.ClosedConnect();
                }
                else
                {
                    //获取字符串
                    string content = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
                    cm.PublishMessage(new { data = content }.ToJson());
                }
            }
        }
Ejemplo n.º 2
0
        //--------------------------------------------------------------------------------

        #region 处理消息
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task ProcessChat(AspNetWebSocketContext context)
        {
            WebSocket   socket = context.WebSocket;
            ChatManager cm     = ChatManager.CreateInstance(_channelid, _imuserid, socket, _sourcetype);

            //连接通知
            cm.SendMessage(NewsTypeHelper.ToConnect(_imuserid, _sourcetype, cm.SocketId));

            //上线前检查是否有中断时的消息
            SendSuspendMsg(cm);

            while (socket.State == WebSocketState.Open)
            {
                ArraySegment <byte>    buffer = new ArraySegment <byte>(new byte[2048]);
                WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, cm.CancelToken);

                if (result.MessageType == WebSocketMessageType.Close || socket.State == WebSocketState.CloseReceived) //如果输入帧为取消帧,发送close命令
                {
                    cm.ClosedConnect();
                }
                else
                {
                    //获取字符串
                    string content = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
                    if (content != "1")
                    {
                        //var lasttime = DateTime.Now.ToUnix();
                        //AddMessageInfo(cm.ImUserId.ToGuid(), content, lasttime);
                        //var data = NewsTypeHelper.ToChat(cm.ImUserId, _sourcetype, content, lasttime);
                        //await cm.PublishMessage(data);
                    }
                    else
                    {
                        cm.SendHeartbeat();//答复心跳
                    }
                }
            }
        }