mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
webpack hot reload support
This commit is contained in:
1 parent
d8e1a63f99
commit
0ee306d0cf
15 files changed
+89
-63
No files matched your search
@@ -1,19 +1,23 @@
|
||||
import SockJS from 'sockjs-client';
|
||||
import Notification from 'vj/components/notification';
|
||||
import Notification from 'vj/components/notification/index';
|
||||
import i18n from 'vj/utils/i18n';
|
||||
|
||||
export default class Sock {
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.reconnect = false;
|
||||
this.isreconnect = false;
|
||||
this.init();
|
||||
this.notification = null;
|
||||
}
|
||||
|
||||
init() {
|
||||
this.sock = new SockJS(this.url);
|
||||
this.sock.onopen = () => {
|
||||
if (this.reconnect) Notification.info(i18n('Reconnected to the server.'));
|
||||
if (this.onopen) this.onopen(this.reconnect);
|
||||
if (this.isreconnect) {
|
||||
this.notification.hide();
|
||||
Notification.info(i18n('Reconnected to the server.'));
|
||||
}
|
||||
if (this.onopen) this.onopen(this.isreconnect);
|
||||
};
|
||||
this.sock.onclose = (code, reason) => {
|
||||
console.warn('Connection closed, ', code, reason);
|
||||
@@ -21,12 +25,29 @@ export default class Sock {
|
||||
if (this.onclose) this.onclose(code, reason);
|
||||
return;
|
||||
}
|
||||
Notification.warn(i18n('Disconnected from the server. Reconnecting...'));
|
||||
this.reconnect = setTimeout(() => { this.init(); }, 3000);
|
||||
if (!this.closed) {
|
||||
if (!this.notification) {
|
||||
this.notification = new Notification({
|
||||
message: i18n('Disconnected from the server. Reconnecting...'),
|
||||
type: 'warn',
|
||||
duration: 0,
|
||||
action: this.init.bind(this),
|
||||
});
|
||||
console.log(this.notification);
|
||||
}
|
||||
this.isreconnect = setTimeout(() => { this.init(); }, 3000);
|
||||
} else {
|
||||
if (this.notification) this.notification.hide();
|
||||
Notification.warn(i18n('Disconnected from the server.'));
|
||||
}
|
||||
if (this.onclose) this.onclose(code, reason);
|
||||
};
|
||||
this.sock.onmessage = (message) => {
|
||||
if (process.env.NODE_ENV !== 'production') console.log('Sock.onmessage: ', message);
|
||||
const msg = JSON.parse(message);
|
||||
if (msg.error === 'PermissionError' || msg.error === 'PrivilegeError') {
|
||||
this.closed = true;
|
||||
}
|
||||
if (this.onmessage) this.onmessage(message);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user