简体   繁体   English

socket.io基本发出和侦听器失败

[英]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. 我有一个我继承的应用程序,它已经在许多事件上运行了套接字IO。 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... 而我的听众只是在主js文件中...

    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. 我正在使用与预先存在的'move'事件相同的基本模式,该事件工作正常 - 由emit传递的未命名对象,然后在on / listener中命名和使用 - 这些都在相同的主js中文件。

    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. 我在调试器中没有得到任何东西,没有任何“符号”事件,但我可以很容易地看到'move'事件内容来回传递。 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). 我已经尝试过使用该事件,但它没有我在该上下文中需要的对象(完全是moveInfo,游戏似乎缺少我需要的细节)。

I'm using Chrome Dev tools for my main dev env, and Firefox for the other connected client. 我正在为我的主要开发环境使用Chrome开发工具,为其他连接的客户端使用Firefox。

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. 我无法看到已经通过'move'事件完成了,我认为我需要做的就是进行调用并配置监听器,无论哪个客户端进行移动都将成为'符号'事件的服务器。

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? 另外,当我调试socket.io时,Chrome Dev断点似乎没有按预期工作 - 这是正常的吗? 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.emit中创建一个断点,所以我可以调查那个上下文中可用的对象,就像我可以用其他函数一样?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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