2017-06-21 267 views
1

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

所以问题是什么原因延迟?或者我该如何避免这种情况。似乎有超时,但我该如何避免这种情况?

客户端(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> 

回答

0

这里我改变了一部分代码。请测试它,并让我知道是否有任何问题发现

<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> 
1

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

再次检查你的python代码,你是否使任何不必要/可选的套接字关闭?试着摆脱他们。

+0

谢谢你这部分解决了问题。现在我可以在第一时间立即发送我的消息。但是,如果我不发送任何消息约10秒钟,然后重新开始发送消息,则需要5秒钟才能发送消息。我只是好奇为什么需要这么长时间。 – Pluxyy

+0

编辑@ Pluxyy –