예제 #1
0
        public void Configure(IApplicationBuilder app)
        {
            app.Map("/socket", socketApp =>
            {
                socketApp.Run(async context =>
                {
                    var badRequest = true;

                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        var webSocket = (WebSocket) null;
                        try
                        {
                            webSocket = await context.WebSockets.AcceptWebSocketAsync();
                            badRequest = false;
                        }
                        catch
                        {
                            badRequest = true;
                        }

                        var loggerFactory = context.ApplicationServices.GetService<ILoggerFactory>();
                        var memoryPool = context.ApplicationServices.GetService<MemoryPool>();

                        var webSocketHandler = new WebSocketHandler(memoryPool, loggerFactory);

                        webSocketHandler.OnMessageTextAction = msg =>
                        {
                            webSocketHandler.SendAsync($"Recieved: {msg}");
                        };

                        // Start the websocket handler so that we can process things over the channel
                        await webSocketHandler.ProcessRequestAsync(webSocket);
                    }

                    if (badRequest)
                    {
                        // Bad Request
                        context.Response.StatusCode = 400;
                        await context.Response.WriteAsync("Bad request :(");
                    }
                });
            });

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
 public SendContext(WebSocketHandler webSocketHandler, ArraySegment<byte> message, WebSocketMessageType messageType, bool endOfMessage)
 {
     Handler = webSocketHandler;
     Message = message;
     MessageType = messageType;
     EndOfMessage = endOfMessage;
 }
 public CloseContext(WebSocketHandler webSocketHandler)
 {
     Handler = webSocketHandler;
 }