AMS55
AMS55

Reputation: 11

How to connect my wearable to a Node.js server on Tizen?

I am having a problem with accessing a certain route on Node.js server from wearable emulator using Tizen.

I tried using alerts to check if the part where the communication is reached (.open and .send) and it seems to reach them perfectly and no errors are detected. I also made sure to add the correct privileges and access.

        function postDataToServer() {
        var xmlHttp = new XMLHttpRequest();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
                if (xmlHttp.status === 200) {
                    alert("data posted successfully..");
                } else {

                }
            }
        };

        xmlHttp.open("PUT", "http://localhost:3004/read_watch",true);
        alert('hi');
        xmlHttp.send(null);
        }

What I should expect is a console.log message on my compiler when a connection to the read_watch is established.

Upvotes: 1

Views: 505

Answers (1)

최영수
최영수

Reputation: 21

I just tested your code snippet on my Tizen wearable emulator with nodejs local server.

My development environment is like below:

  • Tizen Studio v3.3 on Windows7
  • Tizen v5.0 Wearable Emulator on Windows7
  • nodejs server on Ubuntu 16.04.3 LTS

Following is config.xml that has internet privilege:

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" id="http://yourdomain/BasicXHR" version="1.0.0" viewmodes="maximized">
    <tizen:application id="lvdoC7pmtq.BasicXHR" package="lvdoC7pmtq" required_version="4.0"/>
    <content src="index.html"/>
    <feature name="http://tizen.org/feature/screen.size.all"/>
    <icon src="icon.png"/>
    <name>BasicXHR</name>
    <tizen:profile name="wearable"/>
    <tizen:privilege name="http://tizen.org/privilege/internet"/>
</widget>

Following is index.html that has your code and runs on Tizen Wearable Emulator:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Wearable basic template generated by Samsung Wearable Web IDE"/>

    <title>Tizen Wearable Web IDE - Tizen Wearable - Tizen Wearable basic Application</title>

    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script src="js/main.js"></script>
    <script>    
    function postDataToServer() {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
                if (xmlHttp.status === 200) {
                    console.log("data posted successfully.." + xmlHttp.responseText);
                } else {
                    console.log("Error");
                }
            }
        };
        xmlHttp.open("PUT", "http://{My Ubuntu IP Address}:3004/read_watch",true);
        console.log('hi');
        xmlHttp.send(null);
    }
    postDataToServer();
    </script>
</head>

<body>
  <div class=contents>
    <div style='margin:auto;'>
        <span class=content_text id=textbox>Basic</span>
    </div>
  </div>
</body>
</html>

Following is my nodejs code snippet that runs on Ubuntu server:

var http = require('http');
http.createServer(function (req, res) {
  if (req.url == '/read_watch') {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('<html><body><p>got message from Tizen</p></body></html>');
    res.end();
  }
}).listen(3004);

Following is logs on Tizen Wearable Emulator console:

file:///index.html (25) :hi
file:///index.html (18) :data posted successfully..<html><body><p>got message from Tizen</p></body></html>

With the changes, it works fine as we expected. :)

I'm wondering which privilege you specified in config.xml and if there're any network issues.

Upvotes: 2

Related Questions