コード例 #1
0
        public async Task Invoke(HttpContext context)
        {
            WriteRequestParam(context);

            if (context.WebSockets.IsWebSocketRequest)
            {
                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                Console.WriteLine("WebSocket connected");

                string connId = _manager.AddSocket(webSocket);
                await SendConnIdAsync(webSocket, connId);

                await ReceiveMessageAsync(webSocket, async (result, buffer) =>
                {
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
                        Console.WriteLine($"Message received: {message}");
                        await RouteJSONMessageAsync(message);
                        return;
                    }
                    else if (result.MessageType == WebSocketMessageType.Close)
                    {
                        Console.WriteLine("Received close message");

                        string id = _manager.GetAllSockets().FirstOrDefault(s => s.Value == webSocket).Key;

                        _manager.GetAllSockets().TryRemove(id, out WebSocket socket);

                        await socket.CloseAsync(
                            result.CloseStatus.Value,
                            result.CloseStatusDescription,
                            CancellationToken.None);

                        return;
                    }
                });
            }
            else
            {
                Console.WriteLine("Hello from the the 2nd request delegate.");
                await _next(context);
            }
        }
コード例 #2
0
        // This methid will actually keep the connection open. It starts in "Startup.cs" on line 46.
        // In file WebSocketServerMiddlewareExtensions created I an appBuilder "UseWebSocketServer" where I
        // returning this "WebSocketServerMiddleware".
        public async Task InvokeAsync(HttpContext context)
        {
            // Checking to see if it's a WebSocket Request
            if (context.WebSockets.IsWebSocketRequest)
            {
                // If so, create a WebSocket object and WebSocket connection is going to be established.
                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                Console.WriteLine("");
                Console.WriteLine(">>> Agent Connected <<<");

                // Generate a new ConnID (guid)
                string ConnID = _manager.AddSocket(webSocket);

                // Type 'send' to send ip address to the Agent.
                Console.Write("Type 'Send' to send the ip address to the Agent: ");
                Console.ReadLine();

                // ---------------------------------------------------------------------
                // 1) Send IP address to the agent
                string ipAddress = "10.10.13.10";

                // ==========================================================================
                // ==========================================================================

                // >>> HERE I WOULD LIKE TO RECEIVE A HttpPOST request, like the one I get in my controllers <<<

                // ==========================================================================
                // ==========================================================================


                // Send printJobString to the client.
                await SendToAgent(webSocket, ipAddress);

                // --------------------------------------------------------------------
                // 2) Receive the ping response from the Agent.
                await ReceiveAnswerFromAgent(webSocket, async (result, buffer) =>
                {
                    // NOTE: Here I can choose between "Text", "Close" and "Binary".
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        // Convert the answer from bytes to text
                        var answer = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

                        // Write the answer to the console.
                        Console.WriteLine($"Ping status from the Agent: { answer }");

                        return;
                    }
                    // Allow client to close the socket on a nice way.
                    else if (result.MessageType == WebSocketMessageType.Close)
                    {
                        // Write to the console that the client choosed to close the connection.
                        Console.WriteLine("Received 'Close' message.");

                        // Close a connection to the client.
                        await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);

                        return;
                    }
                });
            }
        }