|
|
|
@ -46,45 +46,45 @@ class SocketConnection extends EventEmitter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async start(token) {
|
|
|
|
|
// 组件重新加载时, 如果 socket 存在, 不需要新建
|
|
|
|
|
if (!this.socket) {
|
|
|
|
|
this.socket = new SignalR.HubConnectionBuilder()
|
|
|
|
|
.configureLogging(SignalR.LogLevel.Information)
|
|
|
|
|
.withUrl(
|
|
|
|
|
`/hubs/chathub`, {
|
|
|
|
|
accessTokenFactory: () => token,
|
|
|
|
|
skipNegotiation: true,
|
|
|
|
|
transport: SignalR.HttpTransportType.WebSockets
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
this.socket.on('ForceExist', () => { //可以做相关业务逻辑
|
|
|
|
|
store.dispatch("Logout").then(()=>{
|
|
|
|
|
window.location.reload()
|
|
|
|
|
}).catch(err=>{
|
|
|
|
|
this.$message.error({
|
|
|
|
|
title: '错误',
|
|
|
|
|
description: err.message
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.socket.on('AppendNotice', (notice) => {
|
|
|
|
|
store.commit("APPEND_NOTICE",notice)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.socket.onclose(async () => {
|
|
|
|
|
this.offline = true
|
|
|
|
|
this.emit('onclose')
|
|
|
|
|
await this._initialize()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await this._initialize()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// async start(token) {
|
|
|
|
|
// // 组件重新加载时, 如果 socket 存在, 不需要新建
|
|
|
|
|
// if (!this.socket) {
|
|
|
|
|
// this.socket = new SignalR.HubConnectionBuilder()
|
|
|
|
|
// .configureLogging(SignalR.LogLevel.Information)
|
|
|
|
|
// .withUrl(
|
|
|
|
|
// `/hubs/chathub`, {
|
|
|
|
|
// accessTokenFactory: () => token,
|
|
|
|
|
// skipNegotiation: true,
|
|
|
|
|
// transport: SignalR.HttpTransportType.WebSockets
|
|
|
|
|
// }
|
|
|
|
|
// )
|
|
|
|
|
// .build()
|
|
|
|
|
|
|
|
|
|
// this.socket.on('ForceExist', () => { //可以做相关业务逻辑
|
|
|
|
|
// store.dispatch("Logout").then(()=>{
|
|
|
|
|
// window.location.reload()
|
|
|
|
|
// }).catch(err=>{
|
|
|
|
|
// this.$message.error({
|
|
|
|
|
// title: '错误',
|
|
|
|
|
// description: err.message
|
|
|
|
|
// })
|
|
|
|
|
// })
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
// this.socket.on('AppendNotice', (notice) => {
|
|
|
|
|
// store.commit("APPEND_NOTICE",notice)
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
// this.socket.onclose(async () => {
|
|
|
|
|
// this.offline = true
|
|
|
|
|
// this.emit('onclose')
|
|
|
|
|
// await this._initialize()
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
// await this._initialize()
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
async authenticate(token) {
|
|
|
|
|
await this.start(token)
|
|
|
|
@ -92,11 +92,11 @@ class SocketConnection extends EventEmitter {
|
|
|
|
|
|
|
|
|
|
listen(method) {
|
|
|
|
|
if (this.offline) return
|
|
|
|
|
if (this.listened.some((v) => v === method)) return
|
|
|
|
|
if (this.listened.some(v => v === method)) return
|
|
|
|
|
this.listened.push(method)
|
|
|
|
|
this.one('onstart', () => {
|
|
|
|
|
this.listened.forEach((method) => {
|
|
|
|
|
this.socket.on(method, (data) => {
|
|
|
|
|
this.listened.forEach(method => {
|
|
|
|
|
this.socket.on(method, data => {
|
|
|
|
|
if (this.options.log) {
|
|
|
|
|
console.log({
|
|
|
|
|
type: 'receive',
|
|
|
|
@ -143,7 +143,7 @@ class SocketConnection extends EventEmitter {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
|
|
|
|
return new Promise(async (resolve) => this.one('onstart', () => resolve(this.socket.invoke(methodName, ...args))))
|
|
|
|
|
return new Promise(async resolve => this.one('onstart', () => resolve(this.socket.invoke(methodName, ...args))))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -173,16 +173,16 @@ function install(Vue, connection) {
|
|
|
|
|
if (this.$options.sockets) {
|
|
|
|
|
const methods = Object.getOwnPropertyNames(this.$options.sockets)
|
|
|
|
|
|
|
|
|
|
methods.forEach((method) => {
|
|
|
|
|
methods.forEach(method => {
|
|
|
|
|
Socket.listen(method)
|
|
|
|
|
|
|
|
|
|
Socket.one(method, (data) => this.$options.sockets[method].call(this, data))
|
|
|
|
|
Socket.one(method, data => this.$options.sockets[method].call(this, data))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.$options.subscribe) {
|
|
|
|
|
Socket.one('authenticated', () => {
|
|
|
|
|
this.$options.subscribe.forEach((channel) => {
|
|
|
|
|
this.$options.subscribe.forEach(channel => {
|
|
|
|
|
Socket.invoke('join', channel)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|