0

I am trying to request value from server through socketjs.

Here is 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);
      });
}

and in Server.js

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

  });
});

I can get the value from server, but the problem is I am receiving also the older value. here is the 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

You can see, On the first click, the client receives the perfect value, But on second click, its printing two times, on third click, its printing 3 times. looks like, this function is called three time with previous parameters.

Any Idea?

Shaon Debnath
  • 95
  • 2
  • 10

1 Answers1

0

Not sure about the old values, but one problem that I see, the socket listener in the client "socket.on('resolution'", that one is created multiple times, always when the function OnClick is called. The listener should be created outside of the funtion. Other you will have multiple listeners to the same event. Hope that helps.

bdifferent
  • 703
  • 4
  • 12