Transmitting data continuously from Chrome for Android to Node.js server using Socket.io

馋奶兔 提交于 2019-12-12 15:22:49

问题


I need to transmit the touch position in Chrome for Android continuously to a Node.js server using Socket.io. However, I guess, the transmission is too fast. The first few values receive the server (For the first touch about six values, later only two or three values), then there seems to be a traffic jam. For many seconds the server receives nothing. Then, suddenly all missed values suddenly arrive at the server. But not continuously...

I think I must reduce the traffic from client to server by letting the client only emit in an interval of 500ms. This video demonstrates the last of the following options.

What I've tried so far (without success):

  • Wrapping a setInterval function around the emit function in the client code:

    setInterval( function(){ 
        Socket.emit( 'position', posX )
    }, 500); 
    
  • Regulating the emission with Date.now() in the client code:

    var timestamp = 0;
    
    // ...
    
    if (Date.now() - timestamp >= 500) {
        Socket.emit( 'position', posX );
        timestamp = Date.now();
    }
    
  • Throttling the touchmove event in the client code using the underscore library

    $('#canvas').on("touchmove", _.throttle(function (ev) {
        var e = ev.originalEvent;
        var posX = logCounter + " : " + e.targetTouches[0].pageX;
        $('#div').text(posX);
        socket.emit('position', posX);
        logCounter++;
        return false;
    }, 500));
    

    Let me mention that this snippet logs posX both on a HTML div in the web page AND in the console of the server. The browser logs every 500ms as expected, but the server (through Socket.io) logs only in the beginning as expected, then break, then all missing values at once.

The project is on Github.

Any ideas how to realize a smooth (continuous) transmission?

Edit:

I figured out that this problem only exists with the Chrome browser on my Android phone and on my Android tablet. This application works fine for Firefox for Android, Opera for Android and Safari for iOs.

How can I outsmart Chrome?

来源:https://stackoverflow.com/questions/34295569/transmitting-data-continuously-from-chrome-for-android-to-node-js-server-using-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!