mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
站内消息即时通知,测试断线重连
This commit is contained in:
1 parent
a93da2badb
commit
900e19382b
14 files changed
+120
-46
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { AutoloadPage } from 'vj/misc/PageLoader';
|
||||
import Notification from 'vj/components/notification';
|
||||
import i18n from 'vj/utils/i18n';
|
||||
|
||||
const messagePage = new AutoloadPage('messagePage', () => {
|
||||
async function init(isReconnect = false) {
|
||||
const { default: SockJs } = await import('sockjs-client');
|
||||
|
||||
const sock = new SockJs(`/home/messages-conn?token=${UiContext.token}`);
|
||||
sock.onopen = () => {
|
||||
if (isReconnect) Notification.info('Reconnected');
|
||||
};
|
||||
sock.onmessage = (message) => {
|
||||
const msg = JSON.parse(message.data);
|
||||
console.log(msg);
|
||||
Notification.show({ title: msg.udoc.uname, message: msg.mdoc.content, duration: 0 });
|
||||
};
|
||||
sock.onclose = () => {
|
||||
Notification.warn(i18n('Disconnected from the server. Reconnecting...'));
|
||||
setTimeout(() => {
|
||||
init(true);
|
||||
}, 3000);
|
||||
};
|
||||
}
|
||||
|
||||
if (UserContext._id !== 0) init();
|
||||
});
|
||||
|
||||
export default messagePage;
|
||||
Reference in new issue
Block a user