private async System.Threading.Tasks.Task ProcessRequestAsync(System.Net.HttpListenerContext HttpListenerContext, System.Threading.CancellationToken CancellationToken = default)
        {
            System.Net.WebSockets.WebSocketContext WebSocketContext = null;
            try
            {
                WebSocketContext = await HttpListenerContext.AcceptWebSocketAsync(null);
            }
            catch
            {
                HttpListenerContext.Response.StatusCode = 500;
                HttpListenerContext.Response.Close();
                return;
            }

            SoftmakeAll.SDK.Communication.ServerWebSocket.ConnectionProperties ConnectionProperties = new SoftmakeAll.SDK.Communication.ServerWebSocket.ConnectionProperties(WebSocketContext);

            lock (this.SyncRoot)
                this.ActiveConnections.Add(ConnectionProperties.ConnectionID, ConnectionProperties);

            this.ClientConnected?.Invoke(ConnectionProperties.ConnectionID);

            const System.Int16 BufferSize = 512;

            System.Net.WebSockets.WebSocketReceiveResult Result;
            do
            {
                System.Collections.Generic.IEnumerable <System.Byte> Data = new System.Byte[] { };
                do
                {
                    System.Byte[] Buffer = new System.Byte[BufferSize];
                    Result = await WebSocketContext.WebSocket.ReceiveAsync(new System.ArraySegment <System.Byte>(Buffer), CancellationToken);

                    if (Result.MessageType == System.Net.WebSockets.WebSocketMessageType.Text)
                    {
                        Data = Data.Concat(Buffer);
                    }
                }while (!(Result.EndOfMessage));

                if ((Result.MessageType == System.Net.WebSockets.WebSocketMessageType.Text) && (this.ReceiveMessageFunc != null))
                {
                    await this.ReplyAsync(ConnectionProperties, System.Text.Encoding.UTF8.GetString(Data.SkipLast(BufferSize - Result.Count).ToArray()), CancellationToken);
                }
                else if (Result.MessageType == System.Net.WebSockets.WebSocketMessageType.Close)
                {
                    await this.DropConnectionAsync(ConnectionProperties, true, CancellationToken);
                }
            }while (WebSocketContext.WebSocket.State == System.Net.WebSockets.WebSocketState.Open);
        }
示例#2
0
 private async void AcceptConnection(IAsyncResult ar)
 {
     System.Net.WebSockets.WebSocketContext context = null;
     try
     {
         HttpListenerContext listenerContext = ((HttpListener)ar.AsyncState).EndGetContext(ar);
         if (listenerContext.Request.IsWebSocketRequest)
         {
             try
             {
                 context = await listenerContext.AcceptWebSocketAsync(subProtocol : null);
             }
             catch (Exception ex)
             {
                 listenerContext.Response.StatusCode = 500;
                 listenerContext.Response.Close();
                 Diagnostic.DiagnosticCenter.Instance.Log?.LogException <Exception>(ex);
             }
         }
         else
         {
             listenerContext.Response.StatusCode = 400;
             listenerContext.Response.Close();
         }
     }
     catch (Exception ex)
     {
         Diagnostic.DiagnosticCenter.Instance.Log?.LogException <Exception>(ex);
     }
     finally
     {
         httpListener?.BeginGetContext(new AsyncCallback(AcceptConnection), httpListener);
     }
     if (context != null)
     {
         CollectSocket(context.WebSocket);
     }
 }
示例#3
0
        /// <summary>
        /// Gets the session.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>An object that represents the current content of an http session</returns>
#if NET47
        public SessionInfo GetSession(System.Net.WebSockets.WebSocketContext context)
示例#4
0
        /// <summary>
        /// Gets the session.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="context">The context.</param>
        /// <returns>A session info for the given websocket context</returns>
#if NET47
        public static SessionInfo GetSession(this WebServer server, System.Net.WebSockets.WebSocketContext context)
 public ConnectionProperties(System.Net.WebSockets.WebSocketContext WebSocketContext)
 {
     this.WebSocketContext = WebSocketContext;
     this.LastPingTime     = System.DateTimeOffset.UtcNow;
 }