更改signalR连接
This commit is contained in:
parent
748b9065db
commit
c067951d50
@ -5,7 +5,23 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { mapGetters } from "vuex";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "App",
|
name: "App",
|
||||||
|
computed: {
|
||||||
|
...mapGetters(["token"]),
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
token: {
|
||||||
|
handler: function (val) {
|
||||||
|
if (val) {
|
||||||
|
this.signalr.start();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
deep: true,
|
||||||
|
immediate: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
// 官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio
|
// 官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio
|
||||||
import * as signalR from '@microsoft/signalr'
|
import * as signalR from '@microsoft/signalr'
|
||||||
import store from '../store'
|
import store from '../store'
|
||||||
|
import { getToken } from '@/utils/auth'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
// signalR对象
|
// signalR对象
|
||||||
@ -10,21 +11,24 @@ export default {
|
|||||||
baseUrl: '',
|
baseUrl: '',
|
||||||
init(url) {
|
init(url) {
|
||||||
const connection = new signalR.HubConnectionBuilder()
|
const connection = new signalR.HubConnectionBuilder()
|
||||||
.withUrl(url)
|
.withUrl(url, { accessTokenFactory: () => getToken() })
|
||||||
.build();
|
.build();
|
||||||
// console.log('conn', connection);
|
|
||||||
this.SR = connection;
|
this.SR = connection;
|
||||||
|
|
||||||
// 断线重连
|
// 断线重连
|
||||||
connection.onclose(async () => {
|
connection.onclose(async () => {
|
||||||
await this.SR.start();
|
console.log('断开连接了')
|
||||||
|
await this.start();
|
||||||
})
|
})
|
||||||
|
|
||||||
|
connection.onreconnected(() => {
|
||||||
|
console.log('断线重连')
|
||||||
|
})
|
||||||
connection.on("onlineNum", (data) => {
|
connection.on("onlineNum", (data) => {
|
||||||
store.dispatch("socket/changeOnlineNum", data);
|
store.dispatch("socket/changeOnlineNum", data);
|
||||||
});
|
});
|
||||||
// 启动
|
// 启动
|
||||||
this.start();
|
// this.start();
|
||||||
},
|
},
|
||||||
async start() {
|
async start() {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user