signalr - aspnet/signalr:解析握手响应时出错:TypeError:“instanceof”的右侧不可调用
问题描述
我正在尝试使用 javascript 客户端连接到信号 R 集线器。似乎我能够连接但立即断开连接。
一些日志:
2020-03-11T11:43:47.974Z] 调试:发送握手请求。Utils.js:209 [2020-03-11T11:43:47.978Z] 信息:使用 HubProtocol 'json'。home.component.ts:115 建立与信号 r 的连接时出错 错误:解析握手响应时出错:TypeError:“instanceof”的右侧无法在 HubConnection.push 处调用 ../node_modules/@aspnet/signalr/dist/esm /HubConnection.js.HubConnection.processHandshakeResponse (HubConnection.js:372) 在 HubConnection.push../node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processIncomingData (HubConnection.js:322) 在 WebSocketTransport。 WebSocket.webSocket.onmessage 上的 HubConnection.connection.onreceive (HubConnection.js:65) [as __zone_symbol__ON_PROPERTYmessage] (WebSocketTransport.js:107) 在 WebSocket.wrapFn (zone-evergreen.js:
连接到 SinalR 集线器时出现上述错误。下面是我的客户端代码:
this.hubConnection = new HubConnectionBuilder()
.withUrl('http://localhost:5000/signalr', {
accessTokenFactory: () => localStorage.getItem('token')
})
.configureLogging(LogLevel.Debug)
.build();
this.hubConnection
.start()
.then( () => console.log(this.hubConnection.state))
.catch( error => console.log('Error establishing connection to signal r ', error));
this.hubConnection.on('TotalTrucks', trucks => {
console.log('HERE = ', trucks);
this.tucks = trucks;
});
请注意,我有一个后台服务每 1 分钟向所有连接的客户端发送消息:
public class OtcWorker : BackgroundService
{
private readonly IHubContext<OtcHub> _hubContext;
private IServiceScopeFactory ServiceScopeFactory { get; set; }
public OtcWorker(IHubContext<OtcHub> hubContext, IServiceScopeFactory serviceScopeFactory)
{
_hubContext = hubContext;
ServiceScopeFactory = serviceScopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<WeighbridgeDataContext>();
while (!stoppingToken.IsCancellationRequested)
{
var result = await context.ReceiptTransactions.ToListAsync();
await _hubContext.Clients.All.SendAsync("TotalTrucks", result.Count);
await Task.Delay(3600, stoppingToken);
}
}
}
}
解决方案
你碰巧有一个看起来像的polyfillvar Buffer = Buffer || [];
吗?如果是这样,请删除该行代码。
如果您的 index.html<script>
包含其中
的内容,var Buffer = Buffer || [];
则删除该行代码。这会导致 SignalR 发生握手错误
推荐阅读
- regex - 将正则表达式或 htaccess url.com/city-randomcity/ 重定向到 url.com/randomcity/
- c# - 无法写入自定义事件日志
- java - 即使应用程序未运行,如何每天更新 TextView?
- python - 使用 LidarLite-python 代码进行距离测量
- python - AttributeError:“unicode”对象没有属性“css”
- join - 如何在mysqli中加入一个表,然后在表内加入表?
- node.js - 无法在 nodejs 上启动服务器
- python - 在自定义类型映射中重用 SWIG 映射
- python - 反转数据框中的每 n 行
- python - Django Sqlite3 编程错误