test: Go/JS SDK 接入清单集成测与 README 示例
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { Client } from "../dist/index.js";
|
||||
|
||||
const url = process.env.NIXMSG_URL || "ws://127.0.0.1:7443/mqtt";
|
||||
const id = process.env.NIXMSG_ID || "device-1";
|
||||
const pass = process.env.NIXMSG_PASSWORD || "secret";
|
||||
const peer = process.env.NIXMSG_PEER || "device-2";
|
||||
|
||||
const c = new Client();
|
||||
c.onSessionHandler((tok) => console.log("session", tok));
|
||||
c.onMessageHandler((msg) => console.log("msg", msg.from, msg.body.data));
|
||||
c.onConnectionHandler((ev) => console.log("conn", ev.state, ev.reason));
|
||||
|
||||
await c.connect(url, id, { password: pass });
|
||||
const res = await c.send(
|
||||
{ kind: "endpoint", id: peer },
|
||||
{ enc: "utf8", data: "hello from js sdk" },
|
||||
{ delayMs: 0 },
|
||||
);
|
||||
console.log("sent", res.id, res.state);
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
await c.close();
|
||||
Reference in New Issue
Block a user