Sfoglia il codice sorgente

add: 添加emulator web的测试页面, 仅跑通mqtt, 未对接显示

Wendal Chen 3 anni fa
parent
commit
29f600d140

+ 3 - 3
bsp/emulator/remotem/luat_remotem_main.c

@@ -19,7 +19,7 @@ luat_remotem_ctx_t rctx = {
     .bsp = "win32",
     .wait = 1,
     .mqtt = {
-        .host = "broker-cn.emqx.io",
+        .host = "emqx.site0.cn",
         .port = 1883,
         .protocol = "tcp",
         .topic_uplink = "",
@@ -152,8 +152,8 @@ void luat_remotem_putbuff(char* buff, size_t len) {
 
 cJSON* luat_remotem_json_init(cJSON* top) {
     cJSON_AddNumberToObject(top, "version", 1);
-    cJSON_AddStringToObject(top, "session", "123");
-    cJSON_AddStringToObject(top, "id",      "123");
+    cJSON_AddStringToObject(top, "session", rctx.session_id);
+    cJSON_AddStringToObject(top, "id",      rctx.self_id);
     cJSON_AddStringToObject(top, "role",    "client");
     return cJSON_AddObjectToObject(top,     "data");
 }

+ 46 - 0
bsp/emulator/web/emulator.js

@@ -0,0 +1,46 @@
+const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)
+
+const host = 'wss://emqx.site0.cn/mqtt'
+
+const options = {
+  keepalive: 30,
+  clientId: clientId,
+  protocolId: 'MQTT',
+  protocolVersion: 4,
+  clean: true,
+  reconnectPeriod: 5000,
+  connectTimeout: 30 * 1000,
+  will: {
+    topic: 'WillMsg',
+    payload: 'Connection Closed abnormally..!',
+    qos: 0,
+    retain: false
+  },
+  rejectUnauthorized: false
+}
+
+console.log('connecting mqtt client')
+const client = mqtt.connect(host, options)
+
+client.on('error', (err) => {
+  console.log('Connection error: ', err)
+  client.end()
+})
+
+client.on('reconnect', () => {
+  console.log('Reconnecting...')
+})
+
+client.on('connect', () => {
+  console.log('Client connected:' + clientId)
+  client.subscribe('/sys/luatos/emulator/test/up', { qos: 0 })
+  client.publish('/sys/luatos/emulator/test/up', 'ws connection demo...!', { qos: 0, retain: false })
+})
+
+client.on('message', (topic, message, packet) => {
+  console.log('Received Message: ' + message.toString() + '\nOn topic: ' + topic)
+})
+
+client.on('close', () => {
+  console.log(clientId + ' disconnected')
+})

+ 15 - 0
bsp/emulator/web/index.html

@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>LuatOS模拟器界面</title>
+        <script src="https://unpkg.com/mqtt/dist/mqtt.js"></script>
+        <script>
+          // 将在全局初始化一个 mqtt 变量
+          //console.log(mqtt)
+        </script>
+    </head>
+    <body>
+        <script src="emulator.js"></script>
+        <div></div>
+    </body>
+</html>