1

I'm trying to make an online multiplayer game with express, socket.io, and nodejs. This is my first time using nodejs, and my first time asking a question so if I do anything wrong please tell me. The problem is happening in the sign in part. I've figured out that the client isn't responding to a server emission. I've looked at this question, Socket.IO server not receiving message from client but it didn't help.

This is the relevant server code:

var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket) {
    socket.id = Math.random();
    SOCKET_LIST[socket.id] = socket;

    socket.on('signIn',function(data){
        if(data.username === 'bob' && data.password === "asd"){
            Player.onConnect(socket);
            socket.emit('signInResponse',{success:true});
            console.log('got this far')
        }else {
            socket.emit('signInResponse',{success:false});
        }

    })

This is the relevant client code:

<script>
    var socket = io();
    //sign
    var signDiv = document.getElementById('signDiv');
    var signDivUsername = document.getElementById('signDiv-username');
    var signDivSignIn = document.getElementById('singDiv-singIn');
    var signDivSignUp = document.getElementById('signDiv-singUp');
    var signDivPassword = document.getElementById('signDiv-password');

    signDivSignInfunc = function(){
        console.log('it started')
        socket.emit('signIn',{username:signDivUsername.value,password:signDivPassword.value});
    }
    socket.on('signInResponse',function(data){
        console.log('almost there')
        if(data.success){
            signDiv.style.display = 'none';
            gameDiv.style.display = 'inline-block';
            console.log('worked')
        }else
            alert('Sign in unsuccessful');
    })

Help would be greatly appreciated, thank you.

Tamas Szoke
  • 5,426
  • 4
  • 24
  • 39
  • The bottom one is client code, not server code, sorry. – George Watrous Jul 27 '21 at 18:47
  • Have a look at: https://stackoverflow.com/a/10936512/16471349 And a little searching around "socket io client not receiving events from socket io server" and "socket io events not registered on client" – Manny Jul 27 '21 at 18:57
  • The question may be answered here: https://stackoverflow.com/questions/9837998/socket-io-client-not-receiving-messages-from-server – Manny Jul 27 '21 at 18:59

0 Answers0