Exemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (context.Request.Path == _configuration.GetSection("WebSocketOption:RequestUrlKey").Value)
            {
                //判断是不是websocket
                if (context.WebSockets.IsWebSocketRequest)
                {
                    WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    //客户端第一次连接时
                    var webSocketModel = new WebSocketModel
                    {
                        ConnectionId    = context.Connection.Id,
                        context         = context,
                        webSocket       = webSocket,
                        RemoteIpAddress = context.Connection.RemoteIpAddress,
                        RemotePort      = context.Connection.RemotePort,
                        receiveResult   = new WebSocketReceiveResult(0, messageType, true)
                    };
                    WebSocketHelper.NewSessionConnected(webSocketModel);
                    await Echo(context, webSocket);
                }
            }
            else
            {
                await _next(context);
            }
        }
Exemplo n.º 2
0
        public static bool SendMsgByConnectId(string connectId, byte[] sendBytes)
        {
            WebSocketModel webSocketModel = new WebSocketModel();

            if (keyValuePairs.TryGetValue(connectId, out webSocketModel))
            {
                try
                {
                    webSocketModel.webSocket.SendAsync(new ArraySegment <byte>(sendBytes), WebSocketMessageType.Text, true, CancellationToken.None);
                    return(true);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 广播,给其他在线客户端发消息
 /// </summary>
 /// <param name="msg"></param>
 public static void SendMsgToAllOnlineClient(string ConnectionId, byte[] sendBytes)
 {
     foreach (string item in keyValuePairs.Keys)
     {
         if (item != ConnectionId)
         {
             WebSocketModel myWebSocketModel = new WebSocketModel();
             if (keyValuePairs.TryGetValue(item, out myWebSocketModel))
             {
                 try
                 {
                     myWebSocketModel.webSocket.SendAsync(new ArraySegment <byte>(sendBytes), myWebSocketModel.receiveResult.MessageType, true, CancellationToken.None);
                 }
                 catch (Exception ex)
                 {
                     throw;
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
        private async Task Echo(HttpContext context, WebSocket webSocket)
        {
            int defaultReceiveSize = 4096;

            try
            {
                defaultReceiveSize = Convert.ToInt32(_configuration.GetSection("WebSocketOption:RequestUrlKey").Value);
            }
            catch (Exception) { }
            byte[] buffer = new byte[defaultReceiveSize];
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                byte[] desReceiveBytes = new byte[result.Count];
                Array.Copy(buffer, desReceiveBytes, result.Count);
                var webSocketModel = new WebSocketModel
                {
                    ConnectionId  = context.Connection.Id,
                    context       = context,
                    webSocket     = webSocket,
                    receiveBytes  = desReceiveBytes,
                    receiveResult = result
                };
                WebSocketHelper.NewMessageReceived(webSocketModel);
                result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
            }
            if (result.CloseStatus.HasValue)
            {
                //关闭事件
                var webSocketModel = new WebSocketModel
                {
                    ConnectionId = context.Connection.Id,
                    context      = context,
                    webSocket    = webSocket
                };
                WebSocketHelper.SessionClosed(webSocketModel);
            }
            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 增加在线的客户端
 /// </summary>
 /// <param name="webSocketModel"></param>
 private static void AddOnlineClient(WebSocketModel webSocketModel)
 {
     keyValuePairs.Add(webSocketModel.ConnectionId, webSocketModel);
 }
Exemplo n.º 6
0
 /// <summary>
 /// 客户端断开时
 /// </summary>
 /// <param name="webSocket"></param>
 public static void SessionClosed(WebSocketModel webSocketModel)
 {
     RemoveOnlineClient(webSocketModel);
     SessionClosedEvent(webSocketModel);
 }
Exemplo n.º 7
0
 /// <summary>
 /// 接收到新消息时
 /// </summary>
 /// <param name="webSocket"></param>
 public static void NewMessageReceived(WebSocketModel webSocketModel)
 {
     //自定义业务逻辑写在这里....
     NewMessageReceivedEvent(webSocketModel);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 新客户端连接时
 /// </summary>
 /// <param name="webSocket"></param>
 public static void NewSessionConnected(WebSocketModel webSocketModel)
 {
     AddOnlineClient(webSocketModel);
     //DNLiCore_Utility_Log.FileTxtLogs.WriteLog("客户端连接了连接ID:" + webSocketModel.ConnectionId);
     NewSessionConnectedEvent(webSocketModel);
 }
Exemplo n.º 9
0
 /// <summary>
 /// 移除断开的客户端
 /// </summary>
 /// <param name="webSocketModel"></param>
 private static void RemoveOnlineClient(WebSocketModel webSocketModel)
 {
     keyValuePairs.Remove(webSocketModel.ConnectionId);
 }