.Net Core ClientWebSocket:无法连接到服务器

.Net Core ClientWebSocket: Unable to connect to server

我正在构建一个 Web 应用程序,它连接到 Predix Timeseries 实例以通过 websocket 摄取数据。当我尝试创建套接字时,出现此异常:

System.Net.WebSockets.WebSocketException: 'Unable to connect to the remote server'

我正在使用此代码创建我的 websocket,我的错误在 ConnectAsync 调用中被抛出:

public async Task openWebSocket()
    {
        _socket = new ClientWebSocket();

        _socket.Options.SetRequestHeader(headerName: "predix-zone-id", headerValue: PREDIX_ZONE_ID_HERE);
        _socket.Options.SetRequestHeader(headerName: "authorization", headerValue: "Bearer " + AUTH_TOKEN_HERE);
        _socket.Options.SetRequestHeader(headerName: "content-type", headerValue: "application/json");
        CancellationToken token = new CancellationToken();

        var uri = new Uri(uriString: "wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages");
        await _socket.ConnectAsync(uri: uri, cancellationToken: token);

    }

这是我的异常堆栈跟踪:

System.Net.WebSockets.WebSocketException occurred
  HResult=0x80004005
  Message=Unable to connect to the remote server
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__16.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Services.TimeseriesService.<openWebSocket>d__6.MoveNext() in %PROJECTLOCATION%\Services\%SERVICENAME%.cs:line 34
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Services.TimeseriesService.<Initialize>d__5.MoveNext() in %PROJECTLOCATION%\Services\%SERVICENAME%.cs:line 21
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Program.<Initialize>d__1.MoveNext() in %PROJECTLOCATION%\Program.cs:line 52

Inner Exception 1:
WebSocketException: Unable to connect to the remote server

内部异常堆栈轨道没有更多信息,但注意可能有用的 ThrowOnInvalidConnectState():

    at System.Net.WebSockets.WinHttpWebSocket.ThrowOnInvalidConnectState()
   at System.Net.WebSockets.WinHttpWebSocket.<ConnectAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()

我不确定还能尝试什么 - SO 上的许多其他帖子至少在内部异常中有一些有用的信息。

我也尝试过使用 Fiddler。我可以看到其他服务的消息,包括我从 UAA 检索的身份验证令牌,但此服务没有任何消息。

我是不是遗漏了一些配置步骤?任何帮助或指导将不胜感激。

我没有在 C# 中完成此操作,但 "Unable to connect" 听起来可能是网络问题。你能打开一个 websocket 到另一台服务器吗?也许是这个?

https://www.websocket.org/echo.html

可能是代理服务器问题?您可以尝试使用不同的网络吗?

我想通了。 Predix 时间序列所需的三个 header 是:

  • Predix-Zone-Id: zone_id_here
  • 授权:不记名auth_token_here
  • 来源:“https:// hostname_here

我的问题是我尝试了 Origin header,但是 application_uris 的 Predix VCAP_APPLCIATION 环境变量不包括 "https://"。一旦我手动添加它("https://" + uri)它就可以正常工作。