Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 无法从Angular到NodeJS获取与Socket.IO的连接_Javascript_Node.js_Angular_Websocket_Socket.io - Fatal编程技术网

Javascript 无法从Angular到NodeJS获取与Socket.IO的连接

Javascript 无法从Angular到NodeJS获取与Socket.IO的连接,javascript,node.js,angular,websocket,socket.io,Javascript,Node.js,Angular,Websocket,Socket.io,我花了几个小时试图解决这个问题。。如果有人能帮我。 我不明白为什么它不发送或接收消息 在angular,我得到了这个错误: http://localhost:4200/socket.io/?EIO=3&transport=polling&t=NEhK-JT 404 4200是Angular应用程序的端口,8080是服务器的端口 NodeJS: // Define Porta const port = process.env.PORT || 8080; var server =

我花了几个小时试图解决这个问题。。如果有人能帮我。 我不明白为什么它不发送或接收消息

在angular,我得到了这个错误:

http://localhost:4200/socket.io/?EIO=3&transport=polling&t=NEhK-JT 404
4200是Angular应用程序的端口,8080是服务器的端口

NodeJS:

// Define Porta
const port = process.env.PORT || 8080;
var server = app.listen(port, function () {
    console.log('Server Online - ' + port);
});

// Socket.io
var io = require('socket.io').listen(server);
io.on('connect', function (socket) {
    console.log(`${socket.id} is connected`);
    socket.on('room', function (room) {
        console.log('room', room)
        socket.join(room);
    });
});
角度9:

import * as io from 'socket.io-client'
public socket
public orgId: string = '123abc'

  ngOnInit(): void {
        this.setupSocketConnection();
  }

  chat(nome: string, avatar: number, mensagem: string) {
    io.connect(this.orgId).emit('organizacao', {
      nome: nome,
      mensagem: mensagem,
      avatar: avatar,
    });
  }

  setupSocketConnection() {
    this.socket = io.connect(`http://localhost:8080`, { 
      reconnectionDelay: 1000,
      reconnection: true,
      reconnectionAttempts: 10,
      transports: ['websocket'],
      agent: false, 
      upgrade: false,
      rejectUnauthorized: false
    });
  }
从服务器上的my Console.log

zEnR7Cp23zcur4_kAAAH is connected
86sIiMA8vRZEN-WcAAAI is connected
SU4K2n9jAx_UO2ndAAAJ is connected
UAwlMpNiZWhw_eo9AAAK is connected
K6myruVum4FPKTeLAAAL is connected
Z5QULdZtdsRo5gC1AAAM is connected

如果您试图实施
房间
,请阅读

对于您的情况,我在这里看到的是,在服务器端,您正在收听名为
room
的事件,在客户端,您正在向
organizao
发送消息,并且您还需要使用
socket
对象,而不是
io
,请参见此处


嗨,谢谢你的帮助。当我换成插座时,什么也没发生。你把
organizao
换成
room
了吗?
socket.connect(this.orgId).emit('room', {
      nome: nome,
      mensagem: mensagem,
      avatar: avatar,
    });