onload(() => { const socket = io.connect(document.location.host); socket.on('flash_msg', flash_msg); socket.on('player_state', (data) => { if (!data.secret) { // our secret seems to be to old localStorage.removeItem('secret'); return register(); } //flash_msg("Logged in!"); localStorage.setItem('secret', data.secret); localStorage.setItem('name', data.username); document.cookie = `secret=${data.secret}`; window.username = data.username; $('.username').innerText = 'playing as ' + window.username; }); function register() { const name = localStorage.getItem('name') || prompt("Whats your username?"); socket.emit('signup', name) } if (!localStorage.getItem('secret')) { register() } else { socket.emit('resume', localStorage.getItem('secret')) } window.socket = socket; });