Exemplo n.º 1
0
 public WebSocketClient(HttpContext context, WebSocket webSocket)
 {
     this.ID        = Guid.NewGuid();
     this.WebSocket = webSocket;
     this.Host      = context.Request.Host.Value;
     this.Query     = context.Request.Query.ToDictionary(t => t.Key, t => t.Value.ToString());
     this.Headers   = context.Request.Headers.ToDictionary(t => t.Key, t => t.Value.ToString());
     this.IpAddress = IPAgent.GetIP(context);
     this.Join      = WebAgent.GetTimestamps();
 }
Exemplo n.º 2
0
 public async Task Invoke(HttpContext context)
 {
     if (context.WebSockets.IsWebSocketRequest)
     {
         ws = ToolsFactory.GetWebSocket(context);
         if (ws == null)
         {
             throw new ResultException("不支持WebSocket连接");
         }
         //后台成功接收到连接请求并建立连接后,前台的webSocket.onopen = function (event){}才执行
         WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(true);;
         Dictionary <string, string> data = context.Request.Query.ToDictionary(t => t.Key, t => t.Value.ToString());
         if (!data.ContainsKey("IP"))
         {
             data.Add("IP", IPAgent.GetIP(context));
         }
         if (!data.ContainsKey("IPAddress"))
         {
             data.Add("IPAddress", IPAgent.GetAddress(IPAgent.GetIP(context)));
         }
         wsClient = ws.Register(new WebSocketClient
         {
             ID        = Guid.NewGuid(),
             WebSocket = webSocket,
             Query     = data
         });
         try
         {
             await Handler(wsClient);
         }
         catch (Exception ex)
         {
             await context.Response.WriteAsync(ex.Message).ConfigureAwait(true);;
         }
         finally
         {
             ws.Remove(wsClient);
         }
     }
     else
     {
         await _next(context).ConfigureAwait(true);
     }
 }