Hey,
because your point about the missing events while reconneting makes sense, I put in PING and PONG now. The new code looks now like this:
Summary
const WebSocket = require('ws');
function connect() {
const twitch = new WebSocket('wss://pubsub-edge.twitch.tv');
twitch.on('open', () => {
console.log('Connected with Twitch.');
twitch.ping(JSON.stringify({type: "PING"}), true, function() {console.log('Send PING.')});
setInterval(function() {
setTimeout(function() {
twitch.ping(JSON.stringify({type: "PING"}), true, function() {console.log('Send PING.')});
}, Math.floor((Math.random() * 1000) + 1));
}, 50000)
var noPongRestart = setTimeout(function() {
console.log('Got no PONG.');
twitch.close();
}, 10000);
twitch.on('pong', () => {
console.log('Got PONG.');
clearTimeout(noPongRestart);
})
});
twitch.on('close', () => {
console.log('Disconnected from Twitch.');
connect();
});
};
connect();
The weird thing is: After 60 seconds I still get disconnected. But a PING gets send every 50 seconds. My guess is, that even tho the console tells me I am sending PINGs and getting PONGs, I am actually not really sending a PING. But I seem to have written it right, when I look into the dev doc für a PING. But in case I also tried:
twitch.ping(console.log('Send PING.'));
Same problem.
twitch.ping();
twitch.on('ping', () => {console.log('Send PING.')});
Again, same problem. Also the console doesn’t say Send PING, but a Got PONG. Makes it even weirder.
What did I do wrong?