89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import * as signalR from '@microsoft/signalr';
|
|
import storage from './storage';
|
|
let connection = null as any;
|
|
export const startSignalr = async () => {
|
|
const user = await JSON.parse(storage.getItem('user') as string);
|
|
connection = new signalR.HubConnectionBuilder().withAutomaticReconnect()
|
|
.withUrl(`${import.meta.env.VITE_BASE_URL_API}/session-manage`, {
|
|
skipNegotiation: true,
|
|
transport: signalR.HttpTransportType.WebSockets,
|
|
accessTokenFactory: () => user.token
|
|
})
|
|
.build();
|
|
|
|
connection.start()
|
|
.then(() => console.log('Connection started!'))
|
|
.catch((err: any) => console.error('Error while establishing connection: ', err));
|
|
}
|
|
|
|
export const onInvitation = (callBack: Function) => {
|
|
if (connection) {
|
|
connection.on("Invitation", (roomNum: string, roomName: string, InviterName: string) => {
|
|
callBack({
|
|
key: 'Invitation',
|
|
roomNum, roomName, InviterName
|
|
})
|
|
});
|
|
}
|
|
}
|
|
export const onSignalr = (callBack: Function) => {
|
|
if (connection) {
|
|
connection.on("ReceiveMessage", (uid: string, userName: string, message: string) => {
|
|
callBack({
|
|
key: 'ReceiveMessage',
|
|
uid, message, userName
|
|
})
|
|
});
|
|
connection.on("RefreshUserList", () => {
|
|
callBack({
|
|
key: 'RefreshUserList'
|
|
})
|
|
});
|
|
connection.on("Operation", (type: number) => {
|
|
callBack({
|
|
key: 'Operation',
|
|
type
|
|
})
|
|
});
|
|
connection.on("ForceExitRoom", () => {
|
|
callBack({
|
|
key: 'ForceExitRoom',
|
|
})
|
|
});
|
|
connection.on("RefreshView", (type: string) => {
|
|
callBack({
|
|
key: 'RefreshView',
|
|
type
|
|
})
|
|
});
|
|
}
|
|
}
|
|
export const offSignalr = () => {
|
|
if (connection) {
|
|
connection.off('ReceiveMessage');
|
|
connection.off('RefreshUserList');
|
|
connection.off('Operation');
|
|
connection.off('ForceExitRoom');
|
|
}
|
|
}
|
|
export const onInvoke = async (str: string, data: any) => {
|
|
switch (str) {
|
|
case 'joinChannel':
|
|
await connection.invoke(str, data.roomNum, data.enableMicr, data.enableCamera)
|
|
break;
|
|
case 'levelChannel':
|
|
await connection.invoke(str, data.roomNum)
|
|
break;
|
|
case 'sendChannelMsg':
|
|
await connection.invoke(str, data.roomNum, data.msg)
|
|
break;
|
|
case 'sendOper':
|
|
// 1:全员退出会议
|
|
// 2:设置取消管理员
|
|
// 3:踢出房间
|
|
await connection.invoke(str, data.roomNum, data.type)
|
|
break;
|
|
}
|
|
}
|
|
|