gpt4 book ai didi

Angular 6 SignalR 在启动连接时给出 "WebSocket is not in the OPEN state"

转载 作者:行者123 更新时间:2023-12-02 10:03:18 25 4
gpt4 key购买 nike

我已经在我的 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 未处于打开状态”消息。我哪里做错了?

我遵循的实现示例是 https://code-maze.com/netcore-signalr-angular/

最佳答案

将传输从“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 SignalR 在启动连接时给出 "WebSocket is not in the OPEN state",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768564/

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