49 lines
1.1 KiB
Markdown
49 lines
1.1 KiB
Markdown
# NixMsg JavaScript / TypeScript SDK
|
||
|
||
包名 `@nixevol/nixmsg`。支持 Node.js ≥ 20 与浏览器;发布 ESM 与 CJS。
|
||
|
||
## 安装
|
||
|
||
在 `.npmrc` 中:
|
||
|
||
```ini
|
||
@nixevol:registry=https://git.asio.asia/api/packages/nixevol/npm/
|
||
```
|
||
|
||
然后:
|
||
|
||
```bash
|
||
npm install @nixevol/nixmsg
|
||
```
|
||
|
||
许可证字段为 `SEE LICENSE IN LICENSE`(专有),包内附带仓库根目录 `LICENSE` 副本。
|
||
|
||
## 最小示例
|
||
|
||
见 [`example/minimal.mjs`](./example/minimal.mjs):
|
||
|
||
```js
|
||
import { Client, register } from "@nixevol/nixmsg";
|
||
|
||
const c = new Client();
|
||
c.onSessionHandler((tok) => console.log("session", tok));
|
||
c.onMessageHandler((msg) => console.log("msg", msg.from, msg.body.data));
|
||
await c.connect("ws://127.0.0.1:7443/mqtt", "device-1", { password: "secret" });
|
||
await c.send(
|
||
{ kind: "endpoint", id: "device-2" },
|
||
{ enc: "utf8", data: "hello" },
|
||
{ delayMs: 0 },
|
||
);
|
||
await c.close();
|
||
```
|
||
|
||
浏览器页面与服务器不同源时,注册接口已回 `Access-Control-Allow-Origin: *`,WebSocket `/mqtt` 不校验 Origin,可直接连接。
|
||
|
||
## 打包试运行
|
||
|
||
```bash
|
||
npm pack --dry-run
|
||
```
|
||
|
||
不要对本仓库执行 `npm publish`(发布由阶段 3 总控完成)。
|