[英]How to send WebSocketSubject messages only from client to server?
我使用rxjs的WebSocketSubject为我的websocket连接创建了一个可观察的对象。 到目前为止,服务器与客户端之间的通信正常。 现在的事情是:我无法区分客户端中消息的来源。 我通过在主题上调用next()发送消息。 客户端上的所有订阅也会收到这些消息。 我如何只将消息发送到服务器?
该实现主要源于本文: https : //medium.com/factory-mind/angular-websocket-node-31f421c753ff
我的代码:
socket$: WebSocketSubject<any>;
constructor() {
this.socket$ = WebSocketSubject.create(SOCKET_URL);
this.socket$.subscribe(
(message) => console.log('<-- ' + message),
(err) => console.error('Error on WebSocket:', err),
() => console.warn('Completed!')
);
}
send(message: SocketMessage) {
const tmp: any = {};
tmp.type = message.type;
tmp.payload = message.payload;
// This will be received by the server but also by client subscriptions = bad
this.socket$.next(JSON.stringify(tmp));
}
我找到了一种解决方案,试图让您描述的行为:
调用
next
不会影响WebSocketSubject
订阅者-他们不知道已向服务器发送了某些信息(除非服务器当然会对消息做出响应)。
因此,通过使用RxJS 6而不是RxJS 5,您不应具有上述行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.