|
@@ -11,7 +11,7 @@ export default {
|
|
|
init() {
|
|
|
const token = sessionStorage.getItem('token');
|
|
|
const connection = new signalR.HubConnectionBuilder()
|
|
|
- .withUrl(this.baseUrl, { accessTokenFactory: () => token, skipNegotiation: true, transport: 1 })
|
|
|
+ .withUrl(this.baseUrl, { accessTokenFactory: () => '', skipNegotiation: true, transport: 1 })
|
|
|
.withAutomaticReconnect() //自动重新连接
|
|
|
.configureLogging(signalR.LogLevel.Warning)
|
|
|
.build();
|
|
@@ -31,6 +31,10 @@ export default {
|
|
|
message: `断线重连成功`,
|
|
|
});*/
|
|
|
});
|
|
|
+ // 服务端推送消息
|
|
|
+ connection.on('Send', (message: any) => {
|
|
|
+ console.log('加入分组成功:', message);
|
|
|
+ });
|
|
|
},
|
|
|
/**
|
|
|
* @description 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")})
|
|
@@ -69,11 +73,18 @@ export default {
|
|
|
async joinGroup(groupName: string) {
|
|
|
if (this.SR.state === 'Connected') {
|
|
|
// 判断是否已经建立链接 如果没有 先链接
|
|
|
- await this.SR.invoke('JoinGroupAsync', {GroupName:groupName});
|
|
|
+ await this.SR.invoke('JoinGroupUnauthAsync', {GroupName:groupName});
|
|
|
} else {
|
|
|
+ // 等待链接成功再发送
|
|
|
+ if(this.SR.state === 'Connecting') {
|
|
|
+ setTimeout(async () => {
|
|
|
+ await this.SR.invoke('JoinGroupUnauthAsync', {GroupName:groupName});
|
|
|
+ },500)
|
|
|
+ return
|
|
|
+ }
|
|
|
await this.start().then(async () => {
|
|
|
setTimeout(async () => {
|
|
|
- await this.SR.invoke('JoinGroupAsync', {GroupName:groupName});
|
|
|
+ await this.SR.invoke('JoinGroupUnauthAsync', {GroupName:groupName});
|
|
|
},500)
|
|
|
}).catch((err) => {
|
|
|
console.log(err);
|
|
@@ -90,7 +101,22 @@ export default {
|
|
|
async leaveGroup(groupName: string) {
|
|
|
if (this.SR.state === 'Connected') {
|
|
|
// 判断是否已经建立链接 如果没有 先链接
|
|
|
- await this.SR.invoke('LeaveGroupAsync', {GroupName:groupName});
|
|
|
+ await this.SR.invoke('LeaveGroupUnauthAsync', {GroupName:groupName});
|
|
|
+ } else {
|
|
|
+ // 等待链接成功再发送
|
|
|
+ if(this.SR.state === 'Connecting') {
|
|
|
+ setTimeout(async () => {
|
|
|
+ await this.SR.invoke('LeaveGroupUnauthAsync', {GroupName:groupName});
|
|
|
+ },500)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ await this.start().then(async () => {
|
|
|
+ setTimeout(async () => {
|
|
|
+ await this.SR.invoke('JoinGroupUnauthAsync', {GroupName:groupName});
|
|
|
+ },500)
|
|
|
+ }).catch((err) => {
|
|
|
+ console.log(err);
|
|
|
+ })
|
|
|
}
|
|
|
},
|
|
|
|