# 长连接控制

连接建立时的认证，握手分客户端和设备端，此处为客户端的握手。

 ## getDispatch({region,fullUrl})

功能：获取分配的长连接地址

传参：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| region     | String                    | Y        | 服务器区域         |
| fullUrl    | String                    | Y        | 完整的长连接地址 |

## createHbTimer({hb = 0, hbInterval = 145})

功能：创建一个websocket心跳计时器

## create({connectInfo,onOpen,onClose,onError,onMessage})

功能： 创建一个websocket连接

传参：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| options     | Object                                             | N        | 长连接信息   |
| onOpen          | (_ws: WebSocket) => void                           | Y        | 回调函数   |
| onClose         | () => void                                         | Y        | 回调函数  |
| onError         | (error: ErrorEvent) => void                        | Y        | 回调函数 |
| onMessage       | (_ws: WebSocket, message: MessageEvent) => void: Promise<WebSocket>     | Y        | 回调函数  |

options 字段说明：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| region     | String                    | Y        | 服务器区域        |
| fullUrl    | String                    | Y        | 完整的长连接地址 |
| at         | String                    | N        | 用户token        |
| userApiKey | String                    | N        | 用户 apiKey |
| appId      | String                    | N        | 客户端 appid          |
| userAgent  | String                    | Y        | app 或者 device |

## getUserOnline({connectInfo})

功能：获得用户在线消息

传参：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| connectInfo     | Object                | N        | 长连接信息   |

返回：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| action     | String                    | N        | 固定参数(userOnline)     |
| at         | String                    | N        | 用户 token  |
| apikey     | String                    | N        | 用户 apikey         |
| appid      | String                    | N        | 客户端 appid |
| nonce      | String                    | N        | 8 位字母数字随机数        |
| userAgent  | String                    | N        | app 或者 device  |
| sequence   | String                    | N        | 毫秒级时间戳 |

## getUpdateState({deviceId,params,action,userAgent,userApiKey})

功能：生成更新设备状态指令

传参：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| deviceId     | String                  | N        | 设备 id    |
| params       | Object                  | N        | 设备的状态属性 |
| action       | String                  | Y        | 设备指令 |
| userAgent    | String                  | Y        | app 或者 device |
| userApiKey   | String                  | Y        | 用户 apiKey |

## updateState({deviceId,params,action,userAgent,userApiKey})

功能：更新设备状态

传参：

| 名称       | 类型                      | 允许为空 | 说明                        |
| :--------- | :------------------------ | :------- | :-------------------------- |
| deviceId     | String                  | N        | 设备 id    |
| params       | Object                  | N        | 设备的状态属性   |
| action       | String                  | Y        | 设备指令 |
| userAgent    | String                  | Y        | app 或者 device  |
| userApiKey   | String                  | Y        | 用户 apiKey |

返回：null | undefined
