hunter-client
v0.2.1
Published
## 目的
Downloads
4
Readme
hunter-client
目的
配合hunter
服务,通过websocket采集事件。
通讯结构
{
app_id: number
page: string
package_id: string
package_type: string
event: string
data: {
[key: string]: any
}
}
示例
import { Hunter } from 'hunter-client'
interface PackageMeta {
app_id: number
page: string
package_id: string
package_type: string
}
interface PackageEvent {
event: string
data: {
[key: string]: any
}
}
const hunter = new Hunter<PackageMeta, PackageEvent>()
// 设置接口地址并发起连接
hunter
.setOptions({
serverUrl: 'ws://0.0.0.0/path/to/collector',
token: 'token'
})
.connect()
hunter
.connected() // 连接完毕
.then(() => {
hunter
.updateMeta({ // 更新公共参数
app_id: 1,
package_id: '2',
package_type: 'project',
page: 'project-workbench'
})
.hunt('page.enter', { a: 123 }) // 发送事件
/* |
v
{
"package_id": "2",
"package_type": "project",
"event": "page.enter",
"app_id": 1,
"page": "project-workbench",
"data": {
"a": 123
}
}
*/
})
.catch(() => {}) // 连接失败
hunter
.connected()
.then(() => {
hunter.updateMeta({ // 更新部分参数
...hunter.meta,
package_id: '3'
})
})