尝试通过 signalR 连接到集线器时出错
我尝试连接到集线器并在调用 start(); 时出现异常
未处理的异常:异常:HttpConnection.stopConnection(异常:WebSocket 已关闭,状态代码:1002(null)。)在连接仍处于连接状态时被调用。
这是我的示例代码:
`notificationHubConnection = HubConnectionBuilder().withUrl('${AppAPI.apiNotificationHub}?access_token=$token').withAutomaticReconnect().build();
wait notificationHubConnection!.start();`
有人遇到这个问题吗? 非常感谢 !!!
I try to connect to a hub and get exception on call start();
Unhandled Exception: Exception: HttpConnection.stopConnection(Exception: WebSocket closed with status code: 1002 (null).) was called while the connection is still in the connecting state.
This is my sample code:
`notificationHubConnection = HubConnectionBuilder().withUrl('${AppAPI.apiNotificationHub}?access_token=$token').withAutomaticReconnect().build();
await notificationHubConnection!.start();`
Anyone has this problem ?
Thank you very much !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
HubConnectionBuilder
类之前缺少 new 关键字。我相信这导致了您的问题。试试这个:
注意:
我已使用访问令牌工厂将访问令牌添加到连接中,但如果您愿意,可以将其硬编码在查询字符串中。
(在使用 websocket 传输时,使用访问令牌工厂会将访问令牌添加到查询字符串中,但在使用服务器发送事件或长轮询传输时会添加授权标头。对查询字符串中的令牌进行硬编码会将其保留在查询字符串,无论使用什么传输方式)。
如果错误仍然存在,也许您可以确定您使用的 URL 是否正确?
You are missing the new keyword before the
HubConnectionBuilder
class. I believe this is causing your issue.Try this:
NOTE:
I have used the access token factory to add the access token to the connection, but you can leave it hard coded in the query string if you'd like to.
(Using the access token factory will add the access token to the query string when using the websocket transport, but will add an authorization header when using the Server Sent Events or Long Polling transports. Hard coding the token in the query string will keep it in the query string regardless of the transport used).
If the error persists, perhaps you can make sure the URL you are using is correct?