2

I use "Microsoft.AspNetCore.WebSockets.Server": "0.1.0-rc2-final" to make a websocket server, but not work. (In rc1, it is 1.0.0-rc1-final, and it work fine...)

Here is my code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseWebSockets();

        app.Use(async (http, next) =>
        {

            if (http.WebSockets.IsWebSocketRequest)
            {
                IHandler handler = WebsocketManager.getHandler(http.Request.Path);
                if (handler == null)
                {
                    await next();
                }
                else
                {
                    var webSocket = await http.WebSockets.AcceptWebSocketAsync();
                    if (webSocket != null && webSocket.State == WebSocketState.Open)
                    {
                        // TODO: Handle the socket here.
                        WebsocketManager.Handle(webSocket, handler);
                    }
                }
            }
            else
            {
                // Nothing to do here, pass downstream.  
                await next();
            }
        });


        app.UseMvc();
    }

WebsocketManager is my own manager class. And Here is how I handle websocket session:

    public async Task Listen()
    {
        OnOpen();
        while (_webSocket.State == WebSocketState.Open)
        {
            var error = String.Empty;

            try
            {
                var buffer = new ArraySegment<Byte>(new Byte[4096]);
                var received = await _webSocket.ReceiveAsync(buffer, CancellationToken.None);

                switch (received.MessageType)
                {
                    case WebSocketMessageType.Close:
                        OnClose(received.CloseStatus, received.CloseStatusDescription);
                        break;
                    case WebSocketMessageType.Text:
                        var data = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count).Trim('\0');
                        OnMessage(data);
                        break;
                    case WebSocketMessageType.Binary:
                        OnMessage(buffer.Array);
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                break;
            }
        }
    }

And then, "_webSocket.ReceiveAsync" thrown exception "The request was aborted."

I am sure client not close the session.

How to fix it?

5
  • Are you trying to use SignalR functionality in ASP.NET Core? Commented May 27, 2016 at 14:27
  • @JanshairKhan No, I just want to make a simple websocket listener as game server transport layer.
    – Jin Wei
    Commented May 27, 2016 at 15:43
  • Are you developing and testing on Windows Server 2008 R2 or newer? As far as I know websockets are unsupported on Client OS (at least up to (including) Windows 7. Not sure about Win8/10) and requires Windows Server 2008 R2 or newer. If you run it on an unsupported OS you need to fallback to long polling or similar techniques. SignalR does that, because WebSockets aren't supported everywhere
    – Tseng
    Commented May 27, 2016 at 16:10
  • Could you include all the relevant code (a minimal reproducible example), so that we can replicate the issue?
    – svick
    Commented May 27, 2016 at 18:36
  • fyi - it seems a stream is closed when the initial request is disposed of closing the socket. Same code was fine in RC1 - also why the bump down from 1.0.0-rc1 to 0.1.0-rc2 are we using the wrong package? Commented May 27, 2016 at 21:39

1 Answer 1

4

You need to await this line: WebsocketManager.Handle(webSocket, handler); Otherwise the request ends and the resources get disposed.

1
  • 3
    Can anyone post the fixed full code example (I have trouble implementing it in ASP.NET core) Commented Sep 21, 2016 at 14:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.