I am trying to solve this for hours.. if someone can help me. I cant understand why this doenst emit or receive the message.
At angular I got this error:
http://localhost:4200/socket.io/?EIO=3&transport=polling&t=NEhK-JT 404
4200 is the PORT of my Angular Application and 8080 from my server.
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);
});
});
Angular 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
});
}
From my Console.log at Server
zEnR7Cp23zcur4_kAAAH is connected
86sIiMA8vRZEN-WcAAAI is connected
SU4K2n9jAx_UO2ndAAAJ is connected
UAwlMpNiZWhw_eo9AAAK is connected
K6myruVum4FPKTeLAAAL is connected
Z5QULdZtdsRo5gC1AAAM is connected
If you are trying to implement rooms
then please read https://socket.io/docs/rooms/
For your case what I can see here is that on server-side you are listening to the event named room
and on client-side you are emitting to organizacao
and also you need to use the socket
object instead of io
, refer from here https://www.npmjs.com/package/socket.io-client
socket.connect(this.orgId).emit('room', {
nome: nome,
mensagem: mensagem,
avatar: avatar,
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.