如何将可穿戴设备连接到Tizen上的Node.js服务器?

使用Tizen从可穿戴模拟器访问Node.js服务器上的特定路由时遇到问题。

我尝试使用警报来检查是否可以到达通信的部分(.open和.send),并且似乎可以完美地到达它们并且没有检测到错误。我还确保添加正确的特权和访问权限。

        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);
        }

当建立到read_watch的连接时,我应该在编译器上看到一条console.log消息。

xunying_1 回答:如何将可穿戴设备连接到Tizen上的Node.js服务器?

我刚刚在带有Node.js本地服务器的Tizen可穿戴模拟器上测试了您的代码段。

我的开发环境如下:

  • Windows7上的Tizen Studio v3.3
  • Windows7上的Tizen v5.0可穿戴模拟器
  • Ubuntu 16.04.3 LTS上的nodejs服务器

以下是具有Internet特权的config.xml:

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

以下是具有 您的代码 并在Tizen可穿戴模拟器上运行的index.html:

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

以下是我在Ubuntu服务器上运行的nodejs代码片段:

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

以下是Tizen可穿戴模拟器控制台上的日志:

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

进行更改后,它可以按预期工作。 :)

我想知道您在config.xml中指定了哪种特权,以及是否存在任何网络问题。

本文链接:https://www.f2er.com/3136098.html

大家都在问