首页 > 解决方案 > 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);
            }
        }
    }
}

标签: signalrsignalr.client

解决方案


你碰巧有一个看起来像的polyfillvar Buffer = Buffer || [];吗?如果是这样,请删除该行代码。

如果您的 index.html<script>包含其中 的内容,var Buffer = Buffer || []; 则删除该行代码。这会导致 SignalR 发生握手错误


推荐阅读