// A portable script to find the sender when the server websocket opens (by cursor movement) const vec = require('../utils/vec'); module.exports = function identify(){ // `this` should be Socket instance. // initOnce and init should be functions that act like they describe; initOnce runs once per program execution and init whenever the socket turns back on let self = this; self.on('open', ()=>{ // "Pings" the server with a cursor location which is detected and saved by identity() let coords = [Math.floor(Math.random()*100000+16), Math.floor(Math.random()*1000000+16)]; self.cursor(coords); self.on('cursor', detect); function detect(pos, send){ if (vec.equals(pos[0],coords)){ self.off('cursor', detect); identity(send); } } }); // Calls initOnce and init on successful identification. let initialized = false; function identity(send){ if (! initialized){ initialized = true; self.emit('initOnce'); } self.emit('init', send); } }