socket 断线自动重连,文件上传功能

This commit is contained in:
undefined committed 2020-07-14 16:36:03 +08:00
1 parent 95bfd98b43
commit 10956f1fee
18 files changed
+157 -50

No files matched your search

+8 -14
View File
@@ -1,25 +1,19 @@
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');
async function init() {
const { default: SockJs } = await import('../socket');
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);
Notification.show({
title: msg.udoc.uname,
avatar: msg.udoc.gravatar,
message: msg.mdoc.content,
duration: 0,
});
};
}