Example #1
0
        //处理WebSocket消息
        private async Task Handle(WebSocket_Coon webSocket)
        {
            WebSocket_Coons.Add(webSocket);         //缓存连接对象
            WebSocketReceiveResult result = null;

            do
            {
                //循环接收数据
                var buffer = new byte[1024 * 1];
                result = await webSocket.WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Text && !result.CloseStatus.HasValue)
                {
                    //接收到数据,开始处理
                    var msgString = Encoding.UTF8.GetString(buffer).Trim();
                    try
                    {
                        //解析为WebSocket_Msg对象
                        var msg = JsonConvert.DeserializeObject <WebSocket_Msg>(msgString);
                        if (msg != null && msg.SendClientId == webSocket.ID)
                        {
                            HandleMessage(msg);
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }while (!result.CloseStatus.HasValue);
            WebSocket_Coons.Remove(webSocket);
        }
Example #2
0
        //消息发送
        public static Task SendMessageAsync(WebSocket_Coon coon, string msgStr, string action = "", string msgTag = "")
        {
            if (coon == null)
            {
                return(null);
            }
            WebSocket_Msg pMsg = new WebSocket_Msg()
            {
                Action              = action,
                MsgStr              = msgStr,
                MsgTag              = msgTag,
                SendClientId        = coon.ID,
                SendClientChannelId = coon.Channel
            };

            var msg      = JsonConvert.SerializeObject(pMsg);
            var msgBytes = Encoding.UTF8.GetBytes(msg);

            return(coon.WebSocket.SendAsync(new ArraySegment <byte>(msgBytes, 0, msgBytes.Length), WebSocketMessageType.Text, true, CancellationToken.None));
        }
Example #3
0
        //处理WebSocket初始
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Path == "/ws")
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    //初始新连接对象
                    System.Net.WebSockets.WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    string clientId = Guid.NewGuid().ToString();;
                    var    wsClient = new WebSocket_Coon
                    {
                        ID        = clientId,
                        WebSocket = webSocket
                    };
                    try
                    {
                        //握手通信告知连接ID号
                        await WebSocket_Server.SendMessageAsync(wsClient, "clientId", "handshake", "");

                        //开始处理消息监听
                        await Handle(wsClient);
                    }
                    catch (Exception ex)
                    {
                        await context.Response.WriteAsync("closed");
                    }
                }
                else
                {
                    context.Response.StatusCode = 404;
                }
            }
            else
            {
                await _next(context);
            }
        }
Example #4
0
        public static Task SendMessageAsync(string coonID, string msgStr, string action = "", string msgTag = "")
        {
            WebSocket_Coon coon = WebSocket_Coons.Get(coonID);

            return(SendMessageAsync(coon, msgStr, action, msgTag));
        }
Example #5
0
 //移除WebSocket连接
 public static void Remove(WebSocket_Coon client)
 {
     _clients.Remove(client);
 }
Example #6
0
 //添加WebSocket连接
 public static void Add(WebSocket_Coon client)
 {
     _clients.Add(client);
 }