.Net Core 客户端 WebSocket:无法连接到服务器

3
我正在构建一个web应用程序,通过websocket连接到Predix Timeseries实例以进行数据摄入。当我尝试创建套接字时,会出现以下异常:

System.Net.WebSockets.WebSocketException: '无法连接到远程服务器'

使用以下代码创建我的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检索我的授权令牌,但没有来自该服务的消息。

我错过了某些配置步骤吗?任何帮助或指导将不胜感激。

2个回答

2
我已经找到了问题所在。Predix Timeseries 的三个必需的头文件如下:
  • Predix-Zone-Id: zone_id_here
  • Authorization: Bearer auth_token_here
  • Origin: "https:// hostname_here
我的问题是我尝试使用 Origin 头文件,但是 Predix VCAP_APPLCIATION 环境变量中的 application_uris 不包括 "https://"。一旦我手动添加它("https://" + uri),它就可以正常工作了。

1
我在C#中没有做过这个,但是“无法连接”的提示听起来可能是网络问题。你能否打开一个websocket连接到另一个服务器?也许是这个问题所在?

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

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


我尝试了你的建议,但没有成功 - 不同的网络和websocket测试产生了相同的结果。Predix可能需要连接其他东西吗?我也联系了他们的支持团队,正在等待回复。 - Joe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接