gpt4 book ai didi

javascript - SignalR Javascript 客户端回调未被触发

转载 作者:行者123 更新时间:2023-12-03 00:58:01 25 4
gpt4 key购买 nike

我能够连接到我的集线器,并且我已连接 OnConnected 和 OnDisconnected。他们应该对整数进行加/减,并使用新值调用客户端回调。我的 Angular 应用程序成功连接到服务器,但我注册的回调函数没有被触发。

这是我的服务器中心:

[HubName("online")]
public class OnlineHub : Hub
{
private static int userCount = 0;

public override Task OnConnected()
{
userCount++;
Clients.All.listUpdated(userCount);

return base.OnConnected();
}

public override Task OnDisconnected(bool stopCalled)
{

userCount--;

Clients.All.listUpdated(userCount);

return base.OnDisconnected(stopCalled);
}
}

这是我的 Angular SignalRService:

import { AppSettings } from './../app.settings';
import { EventEmitter, Injectable, OnDestroy } from '@angular/core';

declare const $: any;

@Injectable()
export class SignalRService {

// Declare the variables
private onlineHub: any;
// create the Event Emitter
public messageReceived: EventEmitter<any>;
public connectionEstablished: EventEmitter<Boolean>;
public connectionExists: Boolean;

constructor(private appSettings: AppSettings) {
// Setup
this.connectionEstablished = new EventEmitter<Boolean>();
this.messageReceived = new EventEmitter<any>();
this.connectionExists = false;
}

// This method gets executed from angular controller
public initialize(proxyName: string): void {
this.onlineHub = $.connection.online;

this.onlineHub.client.listUpdated = function(list: any): void {
console.log(list);
this.messageReceived.emit(list);
};

this.startConnection();
}

private startConnection(): void {
$.connection.hub.url = this.appSettings.SIGNALR_BASE_URL + '/signalr';
$.connection.hub.start()
.done((data: any) => {
console.log('SignalR Connected with: ' + data.transport.name);
this.connectionEstablished.emit(true);
this.connectionExists = true;
})
.fail((error: any) => {
console.log('SignalR could not connect: ' + error);
this.connectionEstablished.emit(false);
});
}

private registerOnServerEvents() {
this.onlineHub.client.listUpdated = function(list: any): void {
console.log(list);
this.messageReceived.emit(list);
};
}
}

按照文档所述,我在运行 start() 之前注册回调“listUpdated”,并且 $.connection.hub 在调用 start() 之前包含 client.listUpdated,因此它应该注册。但 OnConnected 方法仍然没有被调用。

最佳答案

我通过将 OnConnected() 和 OnDisconnected() 代码包含在 try/catch block 中解决了这个问题,并创建了一个名为“error”的客户端方法,该方法将最终的异常返回给客户端。这样我发现我遇到了 Json 序列化问题。

我的中心现在看起来像这样:

    [HubName("online")]
public class OnlineHub : Hub
{
private static int userCount = 0;


public override Task OnConnected()
{
try
{
userCount++;
Clients.All.listUpdated(userCount);

}
catch (Exception exc)
{
Clients.All.error(exc);
}

return base.OnConnected();
}

public override Task OnDisconnected(bool stopCalled)
{
try
{
userCount--;
Clients.All.listUpdated(userCount);
}
catch (Exception exc)
{
Clients.All.error(exc);
}

return base.OnDisconnected(stopCalled);
}
}

并且我在调用 start() 之前在 js 客户端上注册错误回调:

this.onlineHub.client.error = (exc: any): void => {
console.log('Error occured:', exc);
};

关于javascript - SignalR Javascript 客户端回调未被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52740560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com