Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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 Socket.io广播到房间不工作_Javascript_Node.js_Angular_Typescript_Socket.io - Fatal编程技术网

Javascript Socket.io广播到房间不工作

Javascript Socket.io广播到房间不工作,javascript,node.js,angular,typescript,socket.io,Javascript,Node.js,Angular,Typescript,Socket.io,在node.js应用程序中使用“socket.io”:“^2.0.4” Socket.io的初始化方式如下: export let io: any; io = socketio(server, { secure: secure, rejectUnauthorized: false, }, ); io.set('origins', '*:*'); io.origins('*:*'); io.on('connection', (sock

在node.js应用程序中使用
“socket.io”:“^2.0.4”

Socket.io的初始化方式如下:

export let io: any;

 io = socketio(server,
    {
      secure: secure,
      rejectUnauthorized: false,
    },
  );

  io.set('origins', '*:*');
  io.origins('*:*');

  io.on('connection', (socket) => {
  socket.on('message', async (data) => {
    }
  }
我想将
消息
广播到房间中的所有插座

所以我做了:

io.to(roomName).emit('message', data);
我在io()之后登录。到().emit()没有问题roomName和数据正常


但是没有事件传入
消息
,我遗漏了什么?

您的socket.IO是否在服务器端正确配置,下面是一个基于rxjs的低级别套接字服务api,可以使用

import { Injectable } from '@angular/core';
import * as socketio from 'socket.io-client';
import {environment} from '../../../environments/environment';
import {Observable} from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';

@Injectable()
export class SocketService {

  private socket: SocketIOClient.Socket;
  connected$ = new BehaviorSubject<boolean>(false);

  constructor() {
    this.socket = socketio(environment.socket.baseUrl, environment.socket.config);
    this.socket.on('connect', () => this.connected$.next(true));
    this.socket.on('disconnect', () => this.connected$.next(false));
  }

  join(room: string) {
    // auto rejoin after reconnect mechanism
    this.connected$.subscribe(connected => {
      if (connected) {
        this.socket.emit('join', {room});
      }
    });
  }

  disconnect() {
    this.socket.disconnect();
    this.connected$.next(false);
  }

  emit(event: string, data?: any) {

    console.group();
      console.log('----- SOCKET OUTBAND -----');
      console.log('Action: ', event);
      console.log('Payload: ', data);
    console.groupEnd();

    this.socket.emit(event, data);
  }

  listen(event: string): Observable<any> {
    return new Observable( observer => {

      this.socket.on(event, data => {

        console.group();
          console.log('----- SOCKET INBOUND -----');
          console.log('Action: ', event);
          console.log('Payload: ', data);
        console.groupEnd();

        observer.next(data);
      });
      // dispose of the event listener when unsubscribed
      return () => this.socket.off(event);
    });
  }

}
从'@angular/core'导入{Injectable};
从“socket.io客户端”导入*作为socketio;
从“../../../environments/environment”导入{environment};
从“rxjs/Observable”导入{Observable};
从'rxjs/BehaviorSubject'导入{BehaviorSubject};
@可注射()
出口级SocketService{
私有套接字:SocketIOClient.socket;
已连接$=新行为主体(false);
构造函数(){
this.socket=socketio(environment.socket.baseUrl,environment.socket.config);
this.socket.on('connect',()=>this.connected$.next(true));
this.socket.on('disconnect',()=>this.connected$.next(false));
}
加入(房间:字符串){
//重新连接后自动重新连接机制
此.connected$.subscribe(connected=>{
如果(已连接){
emit('join',{room});
}
});
}
断开连接(){
this.socket.disconnect();
this.connected$.next(false);
}
发出(事件:字符串,数据?:任意){
console.group();
log('----socketoutband-----');
console.log('操作:',事件);
log('Payload:',data);
console.groupEnd();
this.socket.emit(事件、数据);
}
侦听(事件:字符串):可观察的

尝试以下代码:

socket.broadcast.to(roomName).emit('message', data);

更多信息:

我不能使用该方法,因为我必须在io之外进行调用。广播不是来自socketToo小代码来感知问题。问题是,我也在服务器端配置了socketIo,并且已经在简单的socket-to-socket消息或socket-to-room中使用了它。但是在这种情况下,
io到房间
我不能让它工作,我不明白为什么。