socketjs:从服务器请求值

时间:2018-12-07 02:09:51

标签: socket.io client-server robotjs

我正在尝试通过socketjs从服务器请求值。

这是client.js

function onClick({clientX, clientY}) {      
      console.log("====== #click: "+clickNo);
      clickNo++;
      var srcWidth, srcHeight, mouseClickX, mouseClickY;
      console.log("onmouseEnter: "+clientX +", "+clientY)

      mouseClickX = clientX;
      mouseClickY = clientY;

      socket.emit('screenSize');
      socket.on('resolution', function (data) {
          srcWidth = data.srcWidth;
          srcHeight = data.srcHeight;
          console.log("resolution: "+srcWidth +', '+srcHeight);
          console.log("mouseClick= "+mouseClickX +', '+mouseClickY)
          //moveMouseToPos(mouseClickX, mouseClickY);
      });
}

和在Server.js中

socket.on('screenSize', ()=>{console.log("screenSize Req")
  socket.emit('resolution',{
    srcWidth: robot.getScreenSize().width,
    srcHeight: robot.getScreenSize().height

  });
});

我可以从服务器获取值,但是问题是我也收到了旧值。 这是console.log

====== #click: 0
onmouseEnter: 882, 143
resolution: 1366, 768
mouseClick= 882, 143
====== #click: 1
onmouseEnter: 919, 352
resolution: 1366, 768
mouseClick= 882, 143
resolution: 1366, 768
mouseClick= 919, 352
====== #click: 2
onmouseEnter: 772, 452
resolution: 1366, 768
mouseClick= 882, 143
resolution: 1366, 768
mouseClick= 919, 352
resolution: 1366, 768
mouseClick= 772, 452
====== #click: 3
onmouseEnter: 447, 389
resolution: 1366, 768
mouseClick= 882, 143
resolution: 1366, 768
mouseClick= 919, 352
resolution: 1366, 768
mouseClick= 772, 452
resolution: 1366, 768
mouseClick= 447, 389

您可以看到,在第一次单击时,客户端获得了理想的价值,但是在第二次单击时,客户端打印了两次,第三次单击时,客户端打印了3次。看起来,此函数使用先前的参数调用了三次。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不确定旧值,但是我看到一个问题,即客户端“ socket.on('resolution'”中的套接字侦听器,总是多次调用OnClick函数时,会多次创建该侦听器。应该在该功能之外创建。否则,您将有多个监听同一事件的人。希望有帮助。