客户端未在信号器中接收消息
Clients not receiving message in signalr
我的 signalr 连接客户端没有收到消息。
我的聊天室:
public class Chathub : Hub
{
private readonly string _botUser;
private readonly string _globalRoom;
public Chathub()
{
_botUser = "My Chatbot";
_globalRoom = "Lobby";
}
public async Task Join(string message)
{
//await Groups.AddToGroupAsync(Context.ConnectionId, _globalRoom);
//await Clients.Group(_globalRoom).SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {Context.ConnectionId}"); //Checking connectionid
//await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
}
}
我已经在 SharedService 中定义了我的集线器连接:
export class SharedService {
public readonly URL = "https://localhost:44333/";
private hubConnection: HubConnection;
messageModel: Message = {
message: null
}
constructor(private http: HttpClient) {
this.BuildConnection();
this.StartConnection();
this.JoinMessage();
}
public BuildConnection = () => {
this.hubConnection = new HubConnectionBuilder().withUrl('/chathub').build();
}
public StartConnection = () => {
this.hubConnection.start()
.then(() => console.log("Connection started...."))
.catch(err => console.log(err));
}
LoginUser(UserModel: User): Observable<User> {
this.hubConnection.invoke("Join", UserModel.userName);
return this.http.post<User>(this.URL + "Account/LoginUser", UserModel);
}
JoinMessage(): Observable<string> {
this.hubConnection.on("ReceiveMessage", (messages) => {
this.messageModel.message = messages;
});
return of(this.messageModel.message);
}
我有两个能力。一旦用户登录,hubconnection 就会启动并被路由到另一个显示欢迎消息的组件。
消息显示如下
export class MainPageComponent implements OnInit {
public messages = [];
constructor(public service: SharedService) {}
ngOnInit() {
this.DisplayMessage();
}
public DisplayMessage() {
this.service.JoinMessage().subscribe((data) => this.messages.push(data));
}
}
当我打开两个选项卡并登录时,这两个选项卡之间不共享欢迎消息。第一个登录的客户端没有收到欢迎第二个客户端的消息。我假设 chathub 中的 connectionId 应该相同,以便两个客户端相互通信,但它是不同的。
订阅时你需要return正确的Observable(这里不要使用of()
,使用Subject.next()
)
private message$: Subject<any> = new Subject();
JoinMessage(): Observable<string> {
this.hubConnection.on("ReceiveMessage", (messages) => {
this.message$.next(messages);
});
return this.message$.asObservable();
}
我的 signalr 连接客户端没有收到消息。 我的聊天室:
public class Chathub : Hub
{
private readonly string _botUser;
private readonly string _globalRoom;
public Chathub()
{
_botUser = "My Chatbot";
_globalRoom = "Lobby";
}
public async Task Join(string message)
{
//await Groups.AddToGroupAsync(Context.ConnectionId, _globalRoom);
//await Clients.Group(_globalRoom).SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {Context.ConnectionId}"); //Checking connectionid
//await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
}
}
我已经在 SharedService 中定义了我的集线器连接:
export class SharedService {
public readonly URL = "https://localhost:44333/";
private hubConnection: HubConnection;
messageModel: Message = {
message: null
}
constructor(private http: HttpClient) {
this.BuildConnection();
this.StartConnection();
this.JoinMessage();
}
public BuildConnection = () => {
this.hubConnection = new HubConnectionBuilder().withUrl('/chathub').build();
}
public StartConnection = () => {
this.hubConnection.start()
.then(() => console.log("Connection started...."))
.catch(err => console.log(err));
}
LoginUser(UserModel: User): Observable<User> {
this.hubConnection.invoke("Join", UserModel.userName);
return this.http.post<User>(this.URL + "Account/LoginUser", UserModel);
}
JoinMessage(): Observable<string> {
this.hubConnection.on("ReceiveMessage", (messages) => {
this.messageModel.message = messages;
});
return of(this.messageModel.message);
}
我有两个能力。一旦用户登录,hubconnection 就会启动并被路由到另一个显示欢迎消息的组件。 消息显示如下
export class MainPageComponent implements OnInit {
public messages = [];
constructor(public service: SharedService) {}
ngOnInit() {
this.DisplayMessage();
}
public DisplayMessage() {
this.service.JoinMessage().subscribe((data) => this.messages.push(data));
}
}
当我打开两个选项卡并登录时,这两个选项卡之间不共享欢迎消息。第一个登录的客户端没有收到欢迎第二个客户端的消息。我假设 chathub 中的 connectionId 应该相同,以便两个客户端相互通信,但它是不同的。
订阅时你需要return正确的Observable(这里不要使用of()
,使用Subject.next()
)
private message$: Subject<any> = new Subject();
JoinMessage(): Observable<string> {
this.hubConnection.on("ReceiveMessage", (messages) => {
this.message$.next(messages);
});
return this.message$.asObservable();
}