signalr-no-jquery连接已启动,但未使用websockets调用客户端事件

时间:2018-05-24 13:31:42

标签: asp.net node.js websocket signalr

我正在使用signalr-no-jquery [https://www.npmjs.com/package/signalr-no-jquery] 在Ionic中,API服务器在C#中。 连接从websockets开始,但客户端事件在

时不会被触发
Clients.Client(Context.ConnectionId).broadcast(new { NotificationType = "MESSAGE_RECEIVED" });
在API上的OnConnected()方法上调用

。 以下是代码:

private path: string = 'http://localhost/ABCAPI/';
private proxy: string = 'notificationHub';
private connection: any;
private hubProxy: any;

messageReceived = new EventEmitter<any>();
connectionEstablished = new EventEmitter<boolean>();

connectionExists = false;

constructor() {   

this.connection =  hubConnection(this.path,{
  useDefaultPath: true,
  transport:["serverSentEvents","longPolling"]
  });
this.hubProxy = this.connection.createHubProxy('ABCServer');
this.registerOnserverEvents();
this.startConnection();
}

private startConnection(): void {
this.connection.start()
  .done((data: any) => {
    console.log('SignalR Connected with:  ' + data.transport.name);
    console.log('Now connected, connection ID=' + data.id); 
    this.connectionEstablished.emit(true);
    this.connectionExists = true;
  })
  .fail((error: any) => {
    console.log('SignalR Could not connect: ' + error);
    this.connectionEstablished.emit(false);
}

private registerOnserverEvents(): void {
this.hubProxy.on('broadcast', (data: any) => {
  console.log(data);
  this.messageReceived.emit(data);
});

}

有什么建议吗?

0 个答案:

没有答案