Languages

Menu
Sites
Language
Websockets with Gear S2

Hi, I am new to tizen as I just got a samsung gear S2.

I'm trying to build an app that sends a websocket to a local IP adress but I can't seem to create the socket correctly.

I've tried this code :

    var websocket;  
    var wsUri = "ws://websocket.org/echo.html/";    

    function testWebSocket()
    {
        websocket = new WebSocket(wsUri);
        websocket.onopen = function(evt) { onOpen(evt) };
        websocket.onclose = function(evt) { onClose(evt) };
        websocket.onmessage = function(evt) { onMessage(evt) };
        websocket.onerror = function(evt) { onError(evt) };
     }

      function onOpen(evt)
      {
        console.log("CONNECTED");
        doSend("test");
      }

      function onClose(evt)
      {
        console.log("DISCONNECTED");
      }

      function onMessage(evt)
      {
        console.log(evt.data);
        websocket.close();
      }

      function onError(evt)
      {
        console.log(evt.data);
      }

      function doSend(message)
      {
          if(websocket.readyState === 1){
              console.log("SENT: " + message);
              websocket.send(message);
          }else{
              console.log("Too bad");
          }
      }

       testWebSocket();

as a test  but I get the following message : WebSocket connection to 'ws://websocket.org/echo.html/' failed: Unexpected response code: 301

I then tried with google's url : WebSocket connection to 'ws://google.com/' failed: Unexpected response code: 400   (also tried with wss://)

I also tried to do it with the local ip adr and got : WebSocket connection to 'ws://192.168.1.58:5260/' failed: Unexpected response code: 200
and all other attempt afterwards gave me WebSocket network error: Could not connect to 192.168.1.58: Connection refused
(At this stage I'm pretty sure my server crashed which is why I can't connect anymore).

I made this app on Android studio for another device but I understand sockets and WebSockets are quite different. Can someone tell me what I'm doing wrong ?

 

Responses

8 Replies
Marco Buettner

did u setup the access tag on config.xml and internet privileg?

Aymeri Piganiol

Yup, I put these lines in my xml file :

 <access origin="*" subdomains="true"></access>
<tizen:privilege name="h ttp://tizen.org/privilege/internet"/>
<
feature name="h ttp://tizen.org/feature/network.internet"/>
 

Nafisul Islam Kiron

Hello, did you copy-paste the lines from your xml?

Because there is space in http (h ttp). Did you notice that?

Aymeri Piganiol

No I put the space on purpose because I was told I can't put any external link !

Nafisul Islam Kiron

For web socket tutorial you can see:

http://www.tutorialspoint.com/html5/html5_websocket.htm

https://developer.tizen.org/development/articles/web-sockets   (Will also work on wearables)

*Don't forget to add the required privileges.

Aymeri Piganiol

I'm pretty sure I'd tried this code but tried it again anyway and I still have the same issue.
 

Take the space out of my code, is there any privilege missing from my previous code?

Nafisul Islam Kiron

This worked for me:

<!DOCTYPE HTML>
<html>
   <head>    
      <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");
               
               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:9998/echo");
				
               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };
				
               ws.onmessage = function (evt) 
        <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");
               
               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:9998/echo");
				
               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };
				
               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };
				
               ws.onclose = function()
               { 
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };
            }
            
            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }
      </script>       { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };				
               ws.onclose = function()
               { 
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };
            }            
            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }
      </script>		
   </head>
   <body>   
      <div id="sse">
         <a href="javascript:WebSocketTest()">Run WebSocket</a>
      </div>      
   </body>
</html>

 

Privilege used:

<tizen:privilege name="http://tizen.org/privilege/internet"/>

 

Policy used:

Network URL = "my url" and Allow subdomain = true

 

Hope that helps you :)

Aymeri Piganiol

Okay here is some update : I've tested my code on a browser in my computer and it works fine .

My question now is : Why doesn't it work on my wearable device? Is there some kind of firewall or anything that could prevent me from using websocket?