Files
Yi.Framework/Yi.Bbs.Vue3/src/utils/signalR.js

44 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 官方文档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 useAuths from "@/hooks/useAuths";
const { getToken } = useAuths();
export default {
SR: {},
start(url,callFunc) {
const connection = new signalR.HubConnectionBuilder()
.withUrl(`${import.meta.env.VITE_APP_BASE_WS}/` + url, {
headers: {
Authorization: `Bearer ${getToken()}`,
},
accessTokenFactory: () => {
// 返回授权 token
return `${getToken()}`;
},
})
.withAutomaticReconnect(new ForeverRetryPolicy()) //自动重新连接
.configureLogging(signalR.LogLevel.Error)
.build();
this.SR = connection;
// 断线重连
connection.onclose(() => {
console.log("hub断开");
});
connection.onreconnected(() => {
console.log("hub重新连接成功");
});
callFunc(connection);
// 启动
this.SR.start();
},
};
class ForeverRetryPolicy {
nextRetryDelayInMilliseconds(retryContext) {
return 1000*3;
}
}