260 lines
6.3 KiB
TypeScript
260 lines
6.3 KiB
TypeScript
let connection = '' as any;
|
|
import * as signalR from "signalr-for-wx";
|
|
import { agora } from "./agora";
|
|
export const startSignalr = async (callBack: Function) => {
|
|
wx.getStorage({
|
|
key: "user",
|
|
success(res: any) {
|
|
connection = new signalR.HubConnectionBuilder()
|
|
.withUrl(`https://meeting-api.23544.com/pc/session-manage`, {
|
|
skipNegotiation: true,
|
|
transport: signalR.HttpTransportType.WebSockets,
|
|
accessTokenFactory: () => res.data.token
|
|
})
|
|
.build();
|
|
connection.start().then(() => {
|
|
callBack()
|
|
})
|
|
connection.onclose(() => {
|
|
wx.setStorage({
|
|
key: "reconnect",
|
|
data: false
|
|
})
|
|
startConnection()
|
|
});
|
|
}
|
|
})
|
|
function startConnection() {
|
|
const routes = getCurrentPages()
|
|
if (connection.state !== signalR.HubConnectionState.Connected && routes[routes.length - 1].route === 'pages/meeting/index') {
|
|
connection.start().then(() => {
|
|
wx.setStorage({
|
|
key: "reconnect",
|
|
data: true
|
|
})
|
|
}).catch(err => {
|
|
setTimeout(startConnection, 3000);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
export const onInvoke = async (str: string, data: any) => {
|
|
switch (str) {
|
|
case 'sendChannelMsg':
|
|
await connection.invoke(str, data.roomNum, data.msg)
|
|
break;
|
|
case 'sendOper':
|
|
// 4:屏幕共享
|
|
await connection.invoke(str, data.roomNum, data.type)
|
|
break;
|
|
case 'getDrivers':
|
|
// 获取某个人的设备列表
|
|
await connection.invoke(str, data.uid)
|
|
break;
|
|
case 'sendDrivers':
|
|
// 发送设备列表给某个人
|
|
await connection.invoke(str, data.uid, data.driversJsonString)
|
|
break;
|
|
case 'setDrivers':
|
|
// 设置某个人的设备列表
|
|
await connection.invoke(str, data.uid, data.driversJsonString)
|
|
break;
|
|
case 'joinChannel':
|
|
// 加入房间
|
|
await connection.invoke(str, data.roomNum, data.enableMicr, data.enableCamera, data.isRoomManager || false)
|
|
break;
|
|
case 'levelChannel':
|
|
// 离开房间
|
|
await connection.invoke(str, data.roomNum)
|
|
break;
|
|
}
|
|
}
|
|
export const onSignalr = (callBack: Function) => {
|
|
if (connection) {
|
|
// 聊天
|
|
connection.on("ReceiveMessage", (uid: string, userName: string, message: string, timestamp: string) => {
|
|
callBack({
|
|
key: 'ReceiveMessage',
|
|
uid, message, userName, timestamp
|
|
})
|
|
});
|
|
// 扩展操作
|
|
connection.on("Operation", (type: number) => {
|
|
callBack({
|
|
key: 'Operation',
|
|
type
|
|
})
|
|
});
|
|
// 移出会议
|
|
connection.on("ForceExitRoom", () => {
|
|
callBack({
|
|
key: 'ForceExitRoom',
|
|
})
|
|
});
|
|
// 全员离开房间
|
|
connection.on("AllLeave", () => {
|
|
callBack({
|
|
key: 'AllLeave',
|
|
})
|
|
});
|
|
// 全员看他
|
|
connection.on("ShowUser", (uid: string, uname: string, operUid: string, operUserName: string) => {
|
|
callBack({
|
|
key: 'ShowUser',
|
|
uid,
|
|
uname,
|
|
operUid,
|
|
operUserName,
|
|
})
|
|
});
|
|
// 更新视图模式
|
|
connection.on("RefreshView", (type: string) => {
|
|
callBack({
|
|
key: 'RefreshView',
|
|
type
|
|
})
|
|
});
|
|
// 用户加入频道回调
|
|
connection.on("UserJoined", (user: any) => {
|
|
callBack({
|
|
key: 'UserJoined',
|
|
user,
|
|
})
|
|
});
|
|
// 用户退出频道回调
|
|
connection.on("UserLeave", (uid: string,) => {
|
|
callBack({
|
|
key: 'UserLeave',
|
|
uid,
|
|
})
|
|
});
|
|
// 所有用户开闭麦
|
|
connection.on("OperAllMicr", (enableMicr: boolean, uid: string) => {
|
|
callBack({
|
|
key: 'OperAllMicr',
|
|
enableMicr,
|
|
uid
|
|
})
|
|
});
|
|
// 用户关闭开启麦克风
|
|
connection.on("OperMicr", (user: any, operUid: string) => {
|
|
callBack({
|
|
key: 'OperMicr',
|
|
user,
|
|
operUid
|
|
})
|
|
});
|
|
// 用户开启关闭摄像头
|
|
connection.on("OperCamera", (user: any, operUid: string) => {
|
|
callBack({
|
|
key: 'OperCamera',
|
|
user,
|
|
operUid
|
|
})
|
|
});
|
|
// 发言人用户信息刷新
|
|
connection.on("ManagerRefresh", (user: any, uid: string) => {
|
|
callBack({
|
|
key: 'ManagerRefresh',
|
|
user,
|
|
uid
|
|
})
|
|
});
|
|
// 申请发言
|
|
connection.on("ApplyToSpeak", (uid: string, uname: string) => {
|
|
callBack({
|
|
key: 'ApplyToSpeak',
|
|
uid,
|
|
uname
|
|
})
|
|
});
|
|
// 管理员查看随机用户
|
|
connection.on("Watch", (watchUids: string[]) => {
|
|
callBack({
|
|
key: 'Watch',
|
|
watchUids
|
|
})
|
|
});
|
|
// 设备列表
|
|
connection.on("DriverList", (callerUid: string) => {
|
|
callBack({
|
|
key: 'DriverList',
|
|
callerUid
|
|
})
|
|
});
|
|
// 设置设备
|
|
connection.on("SaveDriver", (driver: string) => {
|
|
callBack({
|
|
key: 'SaveDriver',
|
|
driver
|
|
})
|
|
});
|
|
// 显示设备列表
|
|
connection.on("ShowDriverList", (driversJsonString: string) => {
|
|
callBack({
|
|
key: 'ShowDriverList',
|
|
driversJsonString
|
|
})
|
|
});
|
|
}
|
|
}
|
|
export const onStop = async () => {
|
|
wx.setStorage({
|
|
key: "reconnect",
|
|
data: false
|
|
})
|
|
agora.destroy();
|
|
if (connection) {
|
|
connection.stop()
|
|
connection = '';
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const onOtherSignalr = (callBack: Function) => {
|
|
if (connection) {
|
|
// 邀请
|
|
connection.on("Invitation", (roomNum: string, roomName: string, InviterName: string) => {
|
|
callBack({
|
|
key: 'Invitation',
|
|
roomNum, roomName, InviterName
|
|
})
|
|
});
|
|
// 退出
|
|
connection.on("ForceLogout", (msg: string) => {
|
|
callBack({
|
|
key: 'ForceLogout',
|
|
msg
|
|
})
|
|
});
|
|
}
|
|
}
|
|
export const offSignalr = () => {
|
|
if (connection) {
|
|
connection.off('ReceiveMessage');
|
|
connection.off('Operation');
|
|
connection.off('ForceExitRoom');
|
|
connection.off('AllLeave');
|
|
connection.off('ShowUser');
|
|
connection.off('RefreshView');
|
|
connection.off('UserJoined');
|
|
connection.off('UserLeave');
|
|
connection.off('OperAllMicr');
|
|
connection.off('OperMicr');
|
|
connection.off('OperCamera');
|
|
connection.off('ManagerRefresh');
|
|
connection.off('ApplyToSpeak');
|
|
connection.off('Watch');
|
|
connection.off('DriverList');
|
|
connection.off('SetDriver');
|
|
connection.off('ShowDriverList');
|
|
}
|
|
}
|
|
|