SignalR client connection error (UWP & JavaScript

2019-09-09 22:39发布

I have both, a simple UWP app running on local machine in Visual Studio and a simple AngularJS SPA. I have also an OWIN based self hosted server running in a Service Fabric Cluster. The server use web api and signalR.

When I'm hosting the Service Fabric Cluster on my local machine, the UWP app and the angular app can open a connection to a signalR hub on the server. When I'm hosting the Service Fabric Cluster on Azure, the clients are not able to connect to the signalR hub. I'm getting: failed: Error during WebSocket handshake: Unexpected response code: 400

Server side code in startup class:

        public void Configuration(IAppBuilder appBuilder)
        {
            HttpConfiguration httpConfig = this.ConfigureWebApi();
            FileServerOptions fileServerOptions = this.ConfigureFileSystem(appBuilder);                           appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            appBuilder.UseWebApi(this.ConfigureWebApi());
            appBuilder.UseFileServer(fileServerOptions);
            appBuilder.MapSignalR();

SignalR hub class on server:

    public class SensorDataHub : Hub
    {
        public void UpdateSensorData(SensorDataModel data)
        {
            Clients.All.updateChartData(data);
        }            
    }

UWP code:

        private async void OpenSignalRConnection()
        {    
            //this.SensorDataHubCon = new HubConnection("http://localhost:80/");
            this.SensorDataHubCon = new HubConnection("http://rivutec.westeurope.cloudapp.azure.com/");
            this.SensorDataHubProxy = this.SensorDataHubCon.CreateHubProxy("SensorDataHub");

            await this.SensorDataHubCon.Start();
        }

AngularJS code:

            var connection = $.hubConnection("http://localhost:80/");
            //var connection = $.hubConnection("http://test.server.com/");
            var proxy = connection.createHubProxy("SensorDataHub");

            proxy.on("updateChartData", function (data) {
                console.log(data.current);
            });

            connection.start()
                .done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })
                .fail(function () { console.log('Could not Connect!'); });

As I wrote above, the UWP client and the angular client can open the hub connection to localhost.

But when I'm trying to connect the UWP client to a signalR hub published on a cloud, I'm getting a Bad Request error:

Microsoft.AspNet.SignalR.Client.HttpClientException was unhandled by user code
  HResult=-2146233088
  Message=StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: Microsoft-HTTPAPI/2.0
  Transfer-Encoding: chunked
  Date: Sun, 07 Feb 2016 13:59:09 GMT
  X-Content-Type-Options: nosniff
}
  Source=Microsoft.AspNet.SignalR.Client
  StackTrace:
       at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass6.<Post>b__5(HttpResponseMessage responseMessage)
       at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.<Then>b__17(Task`1 t)
       at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass42.<RunTask>b__41(Task`1 t)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SerialSample.MainPage.<OpenSignalRConnection>d__9.MoveNext()
  InnerException: 

And the signalR trace says:

13:59:02.3510728 - ac120389-8e08-4690-8e38-4538b602260e - SSE: GET http://test.server.com/signalr/connect?clientProtocol=1.4&transport=serverSentEvents&connectionData=[{"Name":"SensorDataHub"}]&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVQGqCkfcnkONWeEawJYNqQAAAAACAAAAAAAQZgAAAAEAACAAAADhW4uMBq5ClXKT3X5UGNGLq3YzdxgD4blCiGSRFL8b8AAAAAAOgAAAAAIAACAAAABGZnghjduUUMHt3QMlhMxIRsajzvG0kWW4ECTpnppl4DAAAAALVIQ46T3%2BdnUpON%2FrpgZO8idns8YNkRrK9lMMxKQz7xYTgM8ViIcxeMNx7%2FamQsRAAAAA5KtFAgG0yKxNVlzAfGNc0cHP8NXxEmaK1i%2Blf52xb7SNmpz%2B5O%2BJxZmVz71Z3pdHV4Z1LyGJixJsRxnQEJOtfA%3D%3D&noCache=28c5a100-2796-4287-958b-0c2ccbd1e91a
The thread 0x4b04 has exited with code 0 (0x0).
13:59:07.3914634 - ac120389-8e08-4690-8e38-4538b602260e - Auto: Failed to connect to using transport serverSentEvents. System.TimeoutException: Transport timed out trying to connect
13:59:07.3994644 - ac120389-8e08-4690-8e38-4538b602260e - LP Connect: http://test.server.com/signalr/connect?clientProtocol=1.4&transport=longPolling&connectionData=[{"Name":"SensorDataHub"}]&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVQGqCkfcnkONWeEawJYNqQAAAAACAAAAAAAQZgAAAAEAACAAAADhW4uMBq5ClXKT3X5UGNGLq3YzdxgD4blCiGSRFL8b8AAAAAAOgAAAAAIAACAAAABGZnghjduUUMHt3QMlhMxIRsajzvG0kWW4ECTpnppl4DAAAAALVIQ46T3%2BdnUpON%2FrpgZO8idns8YNkRrK9lMMxKQz7xYTgM8ViIcxeMNx7%2FamQsRAAAAA5KtFAgG0yKxNVlzAfGNc0cHP8NXxEmaK1i%2Blf52xb7SNmpz%2B5O%2BJxZmVz71Z3pdHV4Z1LyGJixJsRxnQEJOtfA%3D%3D&noCache=78a5c718-439d-440f-9b68-c0658135209f
'SerialSample.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\...\Downloads\samples-develop\samples-develop\SerialSample\CS\bin\x86\Debug\AppX\System.Net.Requests.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
13:59:07.5434999 - ac120389-8e08-4690-8e38-4538b602260e - Auto: Failed to connect to using transport longPolling. Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: Microsoft-HTTPAPI/2.0
  Transfer-Encoding: chunked
  Date: Sun, 07 Feb 2016 13:59:09 GMT
  X-Content-Type-Options: nosniff
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass6.<Post>b__5(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.<Then>b__17(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass42.<RunTask>b__41(Task`1 t)
13:59:07.5755002 - ac120389-8e08-4690-8e38-4538b602260e - OnError(Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: Microsoft-HTTPAPI/2.0
  Transfer-Encoding: chunked
  Date: Sun, 07 Feb 2016 13:59:09 GMT
  X-Content-Type-Options: nosniff
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass6.<Post>b__5(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.<Then>b__17(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass42.<RunTask>b__41(Task`1 t))
13:59:07.6070094 - ac120389-8e08-4690-8e38-4538b602260e - Disconnected
13:59:07.6100082 - ac120389-8e08-4690-8e38-4538b602260e - Transport.Dispose(ac120389-8e08-4690-8e38-4538b602260e)
13:59:07.6135102 - ac120389-8e08-4690-8e38-4538b602260e - Closed
Exception thrown: 'Microsoft.AspNet.SignalR.Client.HttpClientException' in mscorlib.ni.dll

And the angular client error says:

[21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Client subscribed to hub 'sensordatahub'.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Negotiating with 'http://test.server.com//signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D'.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: webSockets transport starting.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Connecting to websocket endpoint 'ws://test.server.com/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVQGqCkfcnkONWeEawJYNqQAAAAACAAAAAAAQZgAAAAEAACAAAAC8DLAeriEnbunGPma3%2F%2FChRl1tm6XCfCHDLEEBfpjszgAAAAAOgAAAAAIAACAAAACANAtQqijBRvg4FloVq0JnylH9%2Bm6j5coY3Yr0yP60mzAAAAD4qw2VdYIQJ3CYQPcPbr2LhyfPhhJtPPgJ33FZuPLQh8owubvHYD5jhRsXdMxHfitAAAAAJX0cLVthvatbmOa%2BNbRSt%2B8CnVJ%2FQ9ks1x%2Bzlk2wA8iF6OVU03wXaNK17FXK2%2BlFmU6hIk8euqJVXeZMz7%2Bfnw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D&tid=4'.
jquery.signalR.min.js:8 WebSocket connection to 'ws://test.server.com/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVQGqCkfcnkONWeEawJYNqQAAAAACAAAAAAAQZgAAAAEAACAAAAC8DLAeriEnbunGPma3%2F%2FChRl1tm6XCfCHDLEEBfpjszgAAAAAOgAAAAAIAACAAAACANAtQqijBRvg4FloVq0JnylH9%2Bm6j5coY3Yr0yP60mzAAAAD4qw2VdYIQJ3CYQPcPbr2LhyfPhhJtPPgJ33FZuPLQh8owubvHYD5jhRsXdMxHfitAAAAAJX0cLVthvatbmOa%2BNbRSt%2B8CnVJ%2FQ9ks1x%2Bzlk2wA8iF6OVU03wXaNK17FXK2%2BlFmU6hIk8euqJVXeZMz7%2Bfnw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D&tid=4' failed: Error during WebSocket handshake: Unexpected response code: 400
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Websocket closed.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Closing the Websocket.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: webSockets transport failed to connect. Attempting to fall back.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: serverSentEvents transport starting.
jquery.signalR.min.js:8 [21:52:32 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Attempting to connect to SSE endpoint 'http://test.server.com/signalr/connect?transport=serv…nw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D&tid=6'.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: serverSentEvents transport timed out when trying to connect.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: EventSource calling close().
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: serverSentEvents transport failed to connect. Attempting to fall back.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: foreverFrame transport starting.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Forever Frame is not supported by SignalR on browsers with SSE support.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: foreverFrame transport failed to connect. Attempting to fall back.
jquery.signalR.min.js:8 [21:52:37 GMT+0100 (Mitteleuropäische Zeit)] SignalR: longPolling transport starting.
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Opening long polling request to 'http://test.server.com/signalr/connect?transport=long…z7%2Bfnw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D'.
jquery.min.js:4 POST http://test.server.com/signalr/connect?transport=long…z7%2Bfnw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D 400 (Bad Request)l.cors.b.crossDomain.send @ jquery.min.js:4n.extend.ajax @ jquery.min.js:4r.transports._logic.ajax @ jquery.signalR.min.js:8e @ jquery.signalR.min.js:8(anonymous function) @ jquery.signalR.min.js:8
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: longPolling transport failed to connect. Attempting to fall back.
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Fallback transports exhausted.
signalRHubService.js:13 Could not Connect!
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Stopping connection.
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: Fired ajax abort async = true.
jquery.signalR.min.js:8 [21:52:38 GMT+0100 (Mitteleuropäische Zeit)] SignalR: LongPolling failed to connect.
jquery.min.js:4 POST http://test.server.com//signalr/abort?transport=longP…z7%2Bfnw%3D%3D&connectionData=%5B%7B%22name%22%3A%22sensordatahub%22%7D%5D 400 (Bad Request)l.cors.b.crossDomain.send @ jquery.min.js:4n.extend.ajax @ jquery.min.js:4r.transports._logic.ajax @ jquery.signalR.min.js:8r.transports._logic.ajaxAbort @ jquery.signalR.min.js:8r.transports.longPolling.abort @ jquery.signalR.min.js:8r.fn.r.stop @ jquery.signalR.min.js:8a.state.r.connectionState.connecting.d @ jquery.signalR.min.js:8v @ jquery.signalR.min.js:8h.transportFailed @ jquery.signalR.min.js:8(anonymous function) @ jquery.signalR.min.js:8y @ jquery.signalR.min.js:8s.pollXhr.i.ajax.error @ jquery.signalR.min.js:8i @ jquery.min.js:2j.fireWith @ jquery.min.js:2z @ jquery.min.js:4(anonymous function) @ jquery.min.js:4

I'm getting failed: Error during WebSocket handshake: Unexpected response code: 400. Why can I connect on localhost, but not on cloud?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-09 23:01

Without seeing more of your code, this is just a WAG, but...

Based on the fact that it was a 'Bad Request' error and the log says it timed out on all the transport types, my instincts are telling me that on your local machine,

  • when you configure the uri for the OWIN self host, you tell it to bind to localhost:80.
  • When the service fabric local cluster starts and runs the Stateful/lessService (or I suppose you could do it with Actors) it instantiates the OWIN self-host
  • OWIN binds to localhost:80 and requests to localhost:80 are correctly routed there
  • Warm fuzzies are had.

    However, when moving the cluster up to Azure:

  • you want to change the OWIN uri address, because localhost is no longer appropriate,

  • so you chose 'test.server.com', or whatever it actually was.
  • Now though, the cluster isn't on a single machine, it's up in Azure and whatever binding you want to use probably isn't valid.
  • Meaning, your OWIN host may be listening for 'test.server.com' requests, but none are coming because no DNS is pointing 'test.server.com' to the service fabric machine(s) hosting the service (which is hosting the OWIN self-host).

    Some documentation I read noted that the service fabric framework has its own name servers which resolve the service fabric references to actual machine address, which is why you refer to service fabric services using the 'fabric:' protocol and let the routing magic happen behind the scenes. So the same limitation that means you can't directly expose an HTTP endpoint for a Service Fabric service without using an intermediary (see this MSDN article for details), is going to limit your ability to self host and bind to a uri that external clients can use.

This article talks about using OWIN to self-host Web API in an Azure Worker role, but the example is limited to using the local emulator and doesn't seem to address the fact that the ip address assigned to the endpoint could and would change in the cloud, making it hard to address reliably on the client.

If you were trying to move the SignalR hub to Service Fabric because you wanted to be able to take advantage of Service Fabric's scalability, check here. The currently recommended/supported path to scale out SignalR that looks like the best fit for what you've got there would be using Web Roles and Azure Service Bus as shown here.

查看更多
登录 后发表回答