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,
});
};
}
+5 -3
View File
@@ -22,16 +22,18 @@ export default class Notification {
}
static show({
avatar, title, message, type, duration = 3000,
avatar, title, message, type = '', duration = 3000,
actionButtons = [],
}) {
if (duration && lastNotification) {
Notification.hide();
}
if (avatar) type += ' avatar';
if (title) type += ' title';
const $dom = $(tpl`<div class="notification ${type} hide"></div>`);
if (avatar) $(tpl`<img width="64" height="64" class="avatar" src="${avatar}"></img>`).appendTo($dom);
if (title) $(tpl`${title} `).appendTo($dom);
$(tpl`${message}`).appendTo($dom);
if (title) $(tpl`<h2>${title}</h2>`).appendTo($dom);
$(tpl`<p>${message}</p>`).appendTo($dom);
actionButtons.forEach((button) => {
$(tpl`<button class="${button.class}" onclick="javascript:window.${button.funcName}();">${button.name}</button>`).appendTo($dom);
});
@@ -14,6 +14,9 @@
box-shadow: 0 4px 10px rgba(#000, 0.4)
min-width: rem(150px)
img
border-radius: 50%
&.hide
opacity: 0
transform: translateY(20px) scale(0.9)
+42
View File
@@ -0,0 +1,42 @@
import SockJS from 'sockjs-client';
import Notification from 'vj/components/notification';
import i18n from 'vj/utils/i18n';
export default class Sock {
constructor(url) {
this.url = url;
this.reconnect = false;
this.init();
}
init() {
this.sock = new SockJS(this.url);
this.sock.onopen = () => {
if (this.reconnect) Notification.warn(i18n('Reconnected to the server.'));
if (this.onopen) this.onopen(this.reconnect);
};
this.sock.onclose = (code, reason) => {
console.warn('Connection closed, ', code, reason);
if (this.closed) {
if (this.onclose) this.onclose(code, reason);
return;
}
Notification.warn(i18n('Disconnected from the server. Reconnecting...'));
this.reconnect = setTimeout(() => { this.init(); }, 3000);
if (this.onclose) this.onclose(code, reason);
};
this.sock.onmessage = (message) => {
if (process.env.NODE_ENV !== 'production') console.log('Sock.onmessage: ', message);
if (this.onmessage) this.onmessage(message);
};
}
send(data) {
this.sock.send(data);
}
close() {
this.closed = true;
this.sock.close();
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ const page = new NamedPage('home_messages', () => {
}
async function mountComponent() {
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const { default: MessagePadApp } = await import('../components/messagepad');
const { default: MessagePadReducer } = await import('../components/messagepad/reducers');
const {
+1 -1
View File
@@ -3,7 +3,7 @@ import { NamedPage } from 'vj/misc/PageLoader';
import * as recordEnum from 'vj/constant/record';
const page = new NamedPage('judge_playground', async () => {
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const sock = new SockJs(`/judge/consume-conn?token=${UiContext.token}`);
sock.onopen = () => {
+1 -1
View File
@@ -1,7 +1,7 @@
import { NamedPage } from 'vj/misc/PageLoader';
const page = new NamedPage('manage_dashboard', async () => {
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const sock = new SockJs(Context.socketUrl);
sock.onopen = () => {
+1 -1
View File
@@ -160,7 +160,7 @@ const page = new NamedPage(['problem_detail', 'contest_detail_problem', 'homewor
if (reactLoaded) return;
$('.loader-container').show();
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const { default: ScratchpadApp } = await import('../components/scratchpad');
const { default: ScratchpadReducer } = await import('../components/scratchpad/reducers');
const {
+1 -1
View File
@@ -1,7 +1,7 @@
import { NamedPage } from 'vj/misc/PageLoader';
const page = new NamedPage('record_detail', async () => {
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const { DiffDOM } = await import('diff-dom');
const sock = new SockJs(Context.socketUrl);
+1 -1
View File
@@ -2,7 +2,7 @@ import { NamedPage } from 'vj/misc/PageLoader';
import UserSelectAutoComplete from 'vj/components/autocomplete/UserSelectAutoComplete';
const page = new NamedPage('record_main', async () => {
const { default: SockJs } = await import('sockjs-client');
const { default: SockJs } = await import('../components/socket');
const { DiffDOM } = await import('diff-dom');
const sock = new SockJs(Context.socketUrl);