Пример #1
0
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task Start(AspNetWebSocketContext context)
        {
            string    socketID = context.QueryString[SOCKETIDNAME].ToString();
            WebSocket socket   = context.WebSocket;

            ContainsKeyInit(socket, socketID);
            ArraySegment <byte> buffer = new ArraySegment <byte>(new byte[2048]);

            #region 离线消息处理
            if (MESSAGE_POOL.ContainsKey(socketID))
            {
                List <SendMessageModel> msgs = MESSAGE_POOL[socketID];
                foreach (SendMessageModel item in msgs)
                {
                    buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(ConvertManager.ModelToJson(item)));
                    await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
                }
                MESSAGE_POOL.Remove(socketID);
            }
            #endregion
            while (true)
            {
                if (socket.State == WebSocketState.Open)
                {
                    WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);

                    string receivedMessage    = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
                    ReceivedMessageModel recM = ConvertManager.JsonToModel <ReceivedMessageModel>(receivedMessage);
                    if (CONNECT_POOL.ContainsKey(recM.TargetSocketID))            //判断客户端是否在线
                    {
                        WebSocket destSocket = CONNECT_POOL[recM.TargetSocketID]; //目的客户端
                        if (destSocket != null && destSocket.State == WebSocketState.Open)
                        {
                            buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(ConvertManager.ModelToJson(new SendMessageModel(recM.Message))));
                            await destSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
                        }
                    }
                    else
                    {
                        await Task.Run(() =>
                        {
                            if (!MESSAGE_POOL.ContainsKey(recM.TargetSocketID))//将用户添加至离线消息池中
                            {
                                MESSAGE_POOL.Add(recM.TargetSocketID, new List <SendMessageModel>());
                            }
                            MESSAGE_POOL[recM.TargetSocketID].Add(new SendMessageModel(recM.Message));//添加离线消息
                        });
                    }
                }
                else
                {
                    if (CONNECT_POOL.ContainsKey(socketID))
                    {
                        CONNECT_POOL.Remove(socketID);
                    }
                    break;
                }
            }
        }
Пример #2
0
 public void GetAnswerSheetResultInfoByIDTest()
 {
     AnswerSheetResultModel resM = _answerSheetBLL.GetAnswerSheetResultInfoByID(Guid.Parse("4A4602E2-C068-4C99-B483-06D849F4472A"));
     string json = ConvertManager.ModelToJson(resM);
 }
Пример #3
0
 /// <summary>
 /// 返回Json字符串
 /// </summary>
 /// <returns>对象Json字符串</returns>
 public string GetJsonStr()
 {
     return(ConvertManager.ModelToJson(this));
 }