Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅从客户端向服务器发送WebSocketSubject消息?_Javascript_Angular_Websocket_Rxjs - Fatal编程技术网

Javascript 如何仅从客户端向服务器发送WebSocketSubject消息?

Javascript 如何仅从客户端向服务器发送WebSocketSubject消息?,javascript,angular,websocket,rxjs,Javascript,Angular,Websocket,Rxjs,我使用rxjs中的WebSocketSubject为我的websocket连接创建了一个可观察的对象。到目前为止,服务器-客户端通信工作正常。现在的问题是:我无法区分我的客户机中消息的来源。我通过调用主题的next()来发送消息。客户端上的所有订阅也会收到这些消息。如何改为只向服务器发送消息 实现主要源于本文: 我的代码: socket$: WebSocketSubject<any>; constructor() { this.socket$ = WebSocketSubjec

我使用rxjs中的WebSocketSubject为我的websocket连接创建了一个可观察的对象。到目前为止,服务器-客户端通信工作正常。现在的问题是:我无法区分我的客户机中消息的来源。我通过调用主题的next()来发送消息。客户端上的所有订阅也会收到这些消息。如何改为只向服务器发送消息

实现主要源于本文:

我的代码:

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));
}
socket$:WebSocketSubject;
构造函数(){
this.socket$=WebSocketSubject.create(socket\uURL);
此.socket$.subscribe(

(message)=>console.log(“我找到了一个解决方案,试图描述您的行为:

声明

调用
next
不会影响
WebSocketSubject
的订阅者-他们没有向服务器发送信息(当然,除非服务器以某种方式响应消息)

因此,通过使用RxJS6而不是RxJS5您不应该具有所描述的行为