简体   繁体   中英

socket.io basic emit and listener failing

I have an App I have inherited which has socket IO working already on a number of events. It's a game that moves pieces on a board and has notation recording the moves. I'm building the notation.

Im trying to create a new emitter/listener pair for the notation, but it fails.

I have the emitter in one file as part of a function which already uses the two objects I need to pass (at the time/context I need to pass them)...

function writeMoveLog(game,moveInfo){
    socket.emit('notation', {game: game, moveInfo: moveInfo});

    (...rest of the function which works ok)

and my listener is simply this in the main js file...

    socket.on('notation', function(gameState){
        writeMoveLog(gameState.game, gameState.moveInfo);    
    });

Basically I'm trying to throw those two objects to the other client end of the socket and have it trigger the function as well, updating the notation. Both clients use the same files.

I'm using the the same basic pattern as the pre-existing 'move' event which works fine - an un-named object passed by the emit, then named and used at the on/listener - these are both in the same main js file.

    socket.emit('move', {
       mInfo: result.mInfo,
       mResult: result,
       senString: gameGen.saveToSEN(game),
       mGame: {contents : game}, //I added this, it works
    });

    socket.on('move', function (move) {
        game = gameGen.loadFromSEN(move.senString);
        gui.draw(game.board);
        etc etc 

I'm not getting anything at all in the debugger, no 'notation' event whatsoever, and yet I can easily see the 'move' event contents going back and forth. I've tried using that event, but it doesn't have the objects I need in that context (moveInfo entirely, and game seems missing detail I need).

I'm using Chrome Dev tools for my main dev env, and Firefox for the other connected client.

What gives? Do I need to register the event somehow somewhere else? I can't see that that has been done with the 'move' event, I thought all I needed to do was make the call and configure the listener, and whichever client is making the move will be the server for the 'notation' event.

On a side note, the Chrome Dev breakpoints don't seem to work as expected when I'm debugging socket.io stuff - is this normal? Why can't I make a breakpoint within a socket.emit so I can investigate the objects which are available in that context, like I can with other functions?

代码没有任何问题,确保您在前端包含了socket.js脚本并且还检查了您的事件是否发生冲突。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM