2

currently I am developing an webapp which will run on a tizen device (Samsung Gear S3). The purpose of that App is to send a message to a websocketserver (written in Python and running on my Computer) when swiping. When I swipe to the right I send a string1 and when I swipe left I send string2. The Problem is that it takes a while until the message arrives to the server (approx. 5 sec). This delay only happend when I run send the message the first time after a while. As far as I could observe that its 10 seconds. That means that I can send any message immidiataly within 10 seconds after the last. But when I Pause for more than 10 seconds there will be a delay of 5 sec.

So the qustion is whats the reason for that delay? Or how can I avoid this. It seems like there is a timeout, but How can I avoid this?

The code on client side (Tizen SmartWatch) is written in Javascript and jquery.

This is the clientsided Code I shorted a little. The HTML Part is not included.

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

2 Answers 2

2

I would suggest to keep the var 'webSocket' declared globally, not inside function scope.

Check also in your python code again, Are you making any unnecessary/optional socket closing ? Try to get rid off them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this solved the problem partially. Now I can send my message immidiately the first time. But if I dont send any message for about 10 seconds and then start again sending messages it takes like 5 seconds to send the message. I am just curious why it takes so long.
Edited @ Pluxyy
1

here i have changed some part of code. please test it and let me know if any issue found

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.