Angular 6 SignalR 在开始连接时给出 "WebSocket is not in the OPEN state"
Angular 6 SignalR gives "WebSocket is not in the OPEN state" on start connection
我已经在我的 Angular 6 项目中安装了@aspnet/signalr
我尝试了在堆栈溢出流程中找到的所有其他选项来解决信号器问题。
以下是我的服务代码:
import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { UserService } from 'app/@core/data/users.service';
@Injectable()
export class SignalRService {
private url = baseurl+'&users='
public data;
private hubConnection: signalR.HubConnection;
constructor(private user: UserService) { }
public startConnection = () => {
let loggedInuser = this.user.getUserEmail();
let accessToken ="some token";
var options = {
transport: signalR.HttpTransportType.WebSockets,
logging: signalR.LogLevel.Trace,
accessTokenFactory: () => accessToken
};
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + loggedInuser , options)
.build();
this.hubConnection.serverTimeoutInMilliseconds = 9999999999999;
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => {
console.log('url: ' + this.url + loggedInuser)
console.log('Error while starting connection: ', err)
})
}
public addTransferChartDataListener = () => {
this.hubConnection.on('AppNotifications', (data) => {
this.data = data;
console.log(data);
});
}
}
当我 运行 通知组件的 ngOninit() 时,我在控制台中收到 "Websocket is not in OPEN state" 消息。
我哪里做错了?
将传输从 'WebSockets' 更改为 'ServerSentEvents',并将 hubConnection.on 移动到 hubConnection.start() 之前
所以,这是我在工作状态下的最终代码
import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { UserService } from 'app/@core/data/users.service';
@Injectable()
export class SignalRService {
private url = baseurl + '&users='
public data;
private hubConnection: signalR.HubConnection;
constructor(private user: UserService) { }
public startConnection = () => {
let loggedInuser = this.user.getUserEmail();
let accessToken = "some token";
var options = {
transport: signalR.HttpTransportType.ServerSentEvents,
logging: signalR.LogLevel.Trace,
accessTokenFactory: () => accessToken
};
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + loggedInuser, options)
.build();
this.hubConnection.serverTimeoutInMilliseconds = 9999999999999;
this.hubConnection.on('AppNotifications', (data: string) => {
this.data = data;
console.log('AppNotifications data');
console.log(data);
});
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => {
console.log('Error while starting connection: ', err)
})
}
public addTransferChartDataListener = () => {
// this.hubConnection.on('AppNotifications', (data) => {
// this.data = data;
// console.log(data);
// });
}
}
我已经在我的 Angular 6 项目中安装了@aspnet/signalr
我尝试了在堆栈溢出流程中找到的所有其他选项来解决信号器问题。 以下是我的服务代码:
import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { UserService } from 'app/@core/data/users.service';
@Injectable()
export class SignalRService {
private url = baseurl+'&users='
public data;
private hubConnection: signalR.HubConnection;
constructor(private user: UserService) { }
public startConnection = () => {
let loggedInuser = this.user.getUserEmail();
let accessToken ="some token";
var options = {
transport: signalR.HttpTransportType.WebSockets,
logging: signalR.LogLevel.Trace,
accessTokenFactory: () => accessToken
};
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + loggedInuser , options)
.build();
this.hubConnection.serverTimeoutInMilliseconds = 9999999999999;
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => {
console.log('url: ' + this.url + loggedInuser)
console.log('Error while starting connection: ', err)
})
}
public addTransferChartDataListener = () => {
this.hubConnection.on('AppNotifications', (data) => {
this.data = data;
console.log(data);
});
}
}
当我 运行 通知组件的 ngOninit() 时,我在控制台中收到 "Websocket is not in OPEN state" 消息。 我哪里做错了?
将传输从 'WebSockets' 更改为 'ServerSentEvents',并将 hubConnection.on 移动到 hubConnection.start() 之前 所以,这是我在工作状态下的最终代码
import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { UserService } from 'app/@core/data/users.service';
@Injectable()
export class SignalRService {
private url = baseurl + '&users='
public data;
private hubConnection: signalR.HubConnection;
constructor(private user: UserService) { }
public startConnection = () => {
let loggedInuser = this.user.getUserEmail();
let accessToken = "some token";
var options = {
transport: signalR.HttpTransportType.ServerSentEvents,
logging: signalR.LogLevel.Trace,
accessTokenFactory: () => accessToken
};
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + loggedInuser, options)
.build();
this.hubConnection.serverTimeoutInMilliseconds = 9999999999999;
this.hubConnection.on('AppNotifications', (data: string) => {
this.data = data;
console.log('AppNotifications data');
console.log(data);
});
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => {
console.log('Error while starting connection: ', err)
})
}
public addTransferChartDataListener = () => {
// this.hubConnection.on('AppNotifications', (data) => {
// this.data = data;
// console.log(data);
// });
}
}