【问题标题】:Websocket delay in sending a messageWebsocket 延迟发送消息
【发布时间】:2017-06-28 02:42:43
【问题描述】:

目前我正在开发一个将在 tizen 设备 (Samsung Gear S3) 上运行的 web 应用程序。该应用程序的目的是在刷卡时向 websocketserver(用 Python 编写并在我的计算机上运行)发送消息。当我向右滑动时,我发送一个 string1,当我向左滑动时,我发送 string2。 问题是消息到达服务器需要一段时间(大约 5 秒)。这种延迟仅在我一段时间后第一次运行发送消息时发生。据我观察,它是 10 秒。这意味着我可以在最后一条消息之后的 10 秒内立即发送任何消息。但是当我暂停超过 10 秒时,会有 5 秒的延迟。

那么问题是延迟的原因是什么?或者我该如何避免这种情况。似乎有超时,但我怎样才能避免这种情况?

客户端(Tizen SmartWatch)的代码是用 Javascript 和 jquery 编写的。

这是我稍微缩短的客户端代码。不包括 HTML 部分。

<script>

var webSocketURL1 = "ws://";
var webSocketURL2 = "Computer10";
var webSocketURL3 = ":9998/echo";

function WS(String){

      var webSocketURL=webSocketURL1+webSocketURL2+webSocketURL3;
      var webSocket = new WebSocket(webSocketURL);
      webSocket.onopen = function (e) {
      console.log('connection open, readyState : ' + e.target.readyState);
      webSocket.send(String); 
    };

     function closeConnection() {
          if (webSocket.readyState === 1) {
              webSocket.close();
          }
      };
}
</script>

<script>
$(document).ready(function(){
  $("body").on("swiperight",function(){     
        WS("string1");
  });                       
});
</script> 

<script>
$(document).ready(function(){
  $("body").on("swipeleft",function(){      
        WS("string2");
    });                       
});
</script>

【问题讨论】:

    标签: javascript websocket delay tizen


    【解决方案1】:

    我建议保留全局声明的 var 'webSocket',而不是在函数范围内。

    再次检查您的python代码,您是否进行了任何不必要/可选的套接字关闭?尝试摆脱它们。

    【讨论】:

    • 谢谢,这部分解决了问题。现在我可以在第一时间立即发送我的信息。但是,如果我在大约 10 秒内不发送任何消息,然后再次开始发送消息,则发送消息大约需要 5 秒。我只是好奇为什么需要这么长时间。
    • 编辑@Pluxyy
    【解决方案2】:

    这里我更改了部分代码。请测试一下,如果发现任何问题,请告诉我

    <script>
        var webSocketURL = 'ws://Computer10:9998/echo';
        var ws;
    
        function connect() {
          //alert('connect');
            ws = new WebSocket(webSocketURL, []);
            // Set the function to be called when a message is received.
            ws.onmessage = handleMessageReceived;
            // Set the function to be called when we have connected to the server.
            ws.onopen = handleConnected;
            // Set the function to be called when an error occurs.
            ws.onerror = handleError;
    
        }
    
        function handleMessageReceived(data) {
            // Simply call logMessage(), passing the received data.
            logMessage(data.data);
        }
    
        function handleConnected(data) {
            // Create a log message which explains what has happened and includes
            // the url we have connected too.
            var logMsg = 'Connected to server: ' + data.target.url;
            // Add the message to the log.
            logMessage(logMsg)
        }
    
        function handleError(err) {
            // Print the error to the console so we can debug it.
            console.log("Error: ", err);
        }
    
        function logMessage(msg) {
            // $apply() ensures that the elements on the page are updated
            // with the new message.
            $scope.$apply(function() {
                //Append out new message to our message log. The \n means new line.
                $scope.messageLog = $scope.messageLog + msg + "\n";
            });
    
        }
    
        $(document).ready(function(){
          $("body").on("swiperight",function(){     
                ws.send("string1");
          });                       
        });
    
        $(document).ready(function(){
          $("body").on("swipeleft",function(){      
                ws.send("string2");
            });                       
        });
    
        connect();
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2014-02-15
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多