示例#1
0
 private static void WebServer_NewRequestReceived(WebAppSession session, HttpRequestInfo requestInfo)
 {
     Task.Run(() =>
     {
         try
         {
             if (session.RequestTime == null)
             {
                 session.RequestTime = DateTime.Now;
             }
             //转发请求
             var host       = requestInfo.HeaderDict["Host"];
             var natSession = NATServer.GetSessions(c => c.MapList?.Any(m => m.remote_endpoint == host) ?? false).FirstOrDefault();
             if (natSession == null)
             {
                 session?.Close();
                 HandleLog.WriteLine($"请求:{host}失败,Nat客户端连接不在线!");
                 return;
             }
             var pack = new PackJson()
             {
                 Host    = host,
                 UserId  = session.UserId,
                 Method  = requestInfo.Method,
                 Route   = requestInfo.Route,
                 Headers = requestInfo.HeaderDict,
                 Content = requestInfo.Content
             };
             var json      = JsonHelper.Instance.Serialize(pack);
             var jsonBytes = Encoding.UTF8.GetBytes(json);
             //02 01 数据长度(4) 正文数据(n)   ---http响应包
             var sendBytes = new List <byte>()
             {
                 0x2, 0x1
             };
             sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse());
             sendBytes.AddRange(jsonBytes);
             natSession.Send(sendBytes.ToArray(), 0, sendBytes.Count);
         }
         catch (Exception ex)
         {
             HandleLog.WriteLine($"【{session.RemoteEndPoint}】请求参数:{Encoding.UTF8.GetString(requestInfo.Data)},处理发生异常:{ex}");
         }
     });
 }
示例#2
0
 private static void WebServer_SessionClosed(WebAppSession session, CSuperSocket.SocketBase.CloseReason value)
 {
     //HandleLog.WriteLine($"客户端【{session.SessionID}】已下线:{value}");
 }
示例#3
0
 private static void WebServer_NewSessionConnected(WebAppSession session)
 {
     //HandleLog.WriteLine($"客户端【{session.SessionID}】已连接");
 }