SignalR WebSockets Angular client - Handshake was canceled, .net core client all ok

See original GitHub issue

Describe the bug

SignalR .Net Core 1 instance hosted on docker. Microsoft.AspNetCore.SignalR 1.1.0 Microsoft.AspNetCore.SignalR.Protocols.MessagePack 1.1.0 Microsoft.AspNetCore.SignalR.StackExchangeRedis 1.1.0

Im use WebSockets, MessagePack. Nginx 1.15 as reverse proxy.

.Net ore client (wpf signalr test app) work fine. Microsoft.AspNetCore.SignalR.Client 1.1.0 Microsoft.AspNetCore.SignalR.Protocols.MessagePack 1.1.0

JS client on angular 7 app throw connection errror. “@aspnet/signalr”: “^1.1.2”, “@aspnet/signalr-protocol-msgpack”: “^1.1.0”,

You can use test server hub https://whitchhunt.ru/api/hubs/hunt - it’s just for fun. Every 1 min he send to all client’s “Notify” message with server time.

Server code:

    public class HuntHub : Hub
    {
        public Task SendToAll(string message)
        {
            return Clients.All.SendAsync("Notify", message);
        }

        public override async Task OnConnectedAsync()
        {
            await Clients.All.SendAsync("Notify", $"{Context.ConnectionId} вошел в чат");
            await base.OnConnectedAsync();
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await Clients.All.SendAsync("Notify", $"{Context.ConnectionId} покинул в чат");
            await base.OnDisconnectedAsync(exception);
        }
    }
            services.AddSignalR(x =>
                {
                    x.EnableDetailedErrors = true;
                    x.HandshakeTimeout = TimeSpan.FromMinutes(1);
                })
                .AddMessagePackProtocol()
                .AddStackExchangeRedis(o =>
            app.UseWebSockets();
            app.UseSignalR(routes =>
            {
                var desiredTransports = HttpTransportType.WebSockets;
                routes.MapHub<HuntHub>("/api/hubs/hunt", options => options.Transports = desiredTransports);
            });

Nginx conf:

  location ~* ^/api/hubs/ {
    resolver 127.0.0.11 valid=10s;
    set $upstream tasks.whitchhunt-api:80;
    proxy_pass   http://$upstream;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

Wpf connection:

            connection = new HubConnectionBuilder()
                .WithUrl("https://whitchhunt.ru/api/hubs/hunt", options => { options.Transports = HttpTransportType.WebSockets; })
                .AddMessagePackProtocol()
                .Build();
            connection.On<string>("Notify", (message) =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    var newMessage = $"Notify {message}";
                    messagesList.Items.Add(newMessage);
                });
            });

Server side log with .net core client:

      OnConnectedAsync started.
dbug: Microsoft.AspNetCore.SignalR.Internal.DefaultHubProtocolResolver[2]
      Found protocol implementation for requested protocol: messagepack.
info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1]
      Completed connection handshake. Using HubProtocol 'messagepack'.

Angular connection:

            this.hubConnection = new HubConnectionBuilder()
                .configureLogging(LogLevel.Debug)
                .withUrl(this.baseUrl + '/api/hubs/hunt', {
                    transport: HttpTransportType.WebSockets,
                })
                .withHubProtocol(new MessagePackHubProtocol())
                .build();
            this.hubConnection.on('Notify', (data: any) => {
                console.log(data);
            });

Angular app log:

[2019-03-15T08:17:51.658Z] Debug: Starting HubConnection.
[2019-03-15T08:17:51.662Z] Debug: Starting connection with transfer format 'Binary'.
[2019-03-15T08:17:51.666Z] Debug: Sending negotiation request: https://whitchhunt.ru/api/hubs/hunt/negotiate.
[2019-03-15T08:17:51.733Z] Debug: Selecting transport 'WebSockets'.
XHR finished loading: POST "https://whitchhunt.ru/api/hubs/hunt/negotiate".
[2019-03-15T08:17:51.955Z] Information: WebSocket connected to wss://whitchhunt.ru/api/hubs/hunt?id=zFq_vKHYJsT9OZ0G5j4IFQ.
[2019-03-15T08:17:51.956Z] Debug: Sending handshake request.
WebSocket is not in the OPEN state
[2019-03-15T08:18:51.950Z] Error: Server returned handshake error: Handshake was canceled.
[2019-03-15T08:18:51.953Z] Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'.
ERROR Error: Uncaught (in promise): Server returned handshake error: Handshake was canceled.

Server side log with js client:

dbug: Microsoft.AspNetCore.SignalR.HubConnectionHandler[5]
      OnConnectedAsync started.
dbug: Microsoft.AspNetCore.SignalR.HubConnectionContext[2]
      Handshake was canceled.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[7]
      Waiting for the client to close the socket.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[2]
      Socket closed.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionManager[2]
      Removing connection zFq_vKHYJsT9OZ0G5j4IFQ from the list of connections.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
analogrelaycommented, Jul 5, 2019

No, the SignalR server components are part of ASP.NET Core and you cannot cross versions like this.

If you are using the SignalR .NET Client you can mix versions like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.net Core SignalR: Server returned Handshake error
I am trying to connect my client application with the IHubContext defined on the server. When I am trying this in my local...
Read more >
ASP.NET Core SignalR connection troubleshooting
This error is usually caused by a client using only the WebSockets transport but the WebSocket protocol isn't enabled on the server.
Read more >
Build Real-time Applications with ASP.NET Core SignalR
Clients attempted to reconnect if a connection was lost, and the server buffered unsent messages and replayed them when a client reconnected.
Read more >
Getting started with SignalR using ASP.NET Core and Angular
This article shows how to setup a first SignalR Hub in ASP.NET Core 2.2 and use it with an Angular client. SignalR was...
Read more >
Asp.Net Core WebSockets Vs SignalR. Which should you use ...
In this video we build 2 separate chat applications, one using Asp. Net Core WebSockets and the other using SignalR, allowing you to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found