Reputation: 1039
I'm trying to build up a Socket.IO connection in my Angular application but just can't get it to work! Here is my current code:
import { io } from 'socket.io-client';
var sock = io('http://my.socket.server', {
reconnectionAttempts: 10,
});
sock.on('connect', () => { console.log('connect'); });
sock.on('reconnect_attempt', () => { console.log('recon'); });
sock.on('reconnecting', () => { console.log('recon'); });
sock.onAny(() => { console.log('any'); });
sock.close();
I can see that the browser is trying to connect and I can even set reconnection attempts - so something seams to work. But none of my on
appender is called! It's probably an absolute trivial issue ...
Thank you!
Edit: Tried this code now and I see no events either:
var sock = io('https://socketio-chat-h9jt.herokuapp.com/', {
reconnectionAttempts: 5,
});
sock.on('connection', sock => {
alert(1);
sock.on('connect', () => { console.log('connect'); });
sock.on('reconnect_attempt', () => { console.log('recon'); });
sock.on('reconnecting', () => { console.log('recon'); });
sock.onAny(() => { console.log('any'); });
sock.close();
});
Upvotes: 0
Views: 392
Reputation: 451
If this is the client side then, according to the socket.io documentation https://socket.io/docs/v3/index.html, the syntax is supposed to go like
const io = require('socket.io')(3000);
io.on('connection', socket => {
socket.on('message', (data) => {
console.log(data);
});
});
so all the socket.on functions should be inside a bigger io.on('connection') function.
Upvotes: 1