|
@@ -13,8 +13,10 @@ enum ReadyState {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export enum CmdType {
|
|
export enum CmdType {
|
|
|
- /** 心跳 */
|
|
|
|
|
|
|
+ /** @name 心跳 */
|
|
|
HEART_BEAT = 0,
|
|
HEART_BEAT = 0,
|
|
|
|
|
+ /** @name 初始化接收的消息 */
|
|
|
|
|
+ FirstMsg = 1,
|
|
|
/** @name 通知、消息 */
|
|
/** @name 通知、消息 */
|
|
|
NoticeOrMessage = 13,
|
|
NoticeOrMessage = 13,
|
|
|
/** @name 在线员工列表 */
|
|
/** @name 在线员工列表 */
|
|
@@ -36,6 +38,7 @@ interface Options {
|
|
|
|
|
|
|
|
class Ws {
|
|
class Ws {
|
|
|
private uid: string // 用户id
|
|
private uid: string // 用户id
|
|
|
|
|
+ private cid: string // 副本号
|
|
|
public uri: string // websocket 链接地址
|
|
public uri: string // websocket 链接地址
|
|
|
public opts?: Options // websocket 链接相关配置
|
|
public opts?: Options // websocket 链接相关配置
|
|
|
private isLock: boolean // 断开锁
|
|
private isLock: boolean // 断开锁
|
|
@@ -84,7 +87,12 @@ class Ws {
|
|
|
if (msg.data === 'pong') {
|
|
if (msg.data === 'pong') {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- this.opts?.onMessage?.(JSON.parse(msg.data))
|
|
|
|
|
|
|
+ const data = JSON.parse(msg.data)
|
|
|
|
|
+ if (data.cmd === CmdType.FirstMsg) {
|
|
|
|
|
+ // 保存当前socket副本号
|
|
|
|
|
+ this.cid = data.CopyCount
|
|
|
|
|
+ }
|
|
|
|
|
+ this.opts?.onMessage?.(data)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.wsIns.onopen = event => {
|
|
this.wsIns.onopen = event => {
|
|
@@ -117,8 +125,15 @@ class Ws {
|
|
|
|
|
|
|
|
/** 发送消息 */
|
|
/** 发送消息 */
|
|
|
public sendMessage(map: MessageType) {
|
|
public sendMessage(map: MessageType) {
|
|
|
|
|
+ const cMap = { ...map }
|
|
|
|
|
+ const keys = Object.keys(map)
|
|
|
|
|
+ keys.forEach(item => {
|
|
|
|
|
+ if (/(userid)|(dstid)/.test(item)) {
|
|
|
|
|
+ cMap[item] += `_${this.cid}`
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
if (this.wsIns?.readyState === ReadyState.Open) {
|
|
if (this.wsIns?.readyState === ReadyState.Open) {
|
|
|
- this.wsIns.send(JSON.stringify(map))
|
|
|
|
|
|
|
+ this.wsIns.send(JSON.stringify(cMap))
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|