Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit e0ae2e0

Browse files
author
bhasher
committed
Client-side connection lost
1 parent 6fb0248 commit e0ae2e0

File tree

4 files changed

+132
-8
lines changed

4 files changed

+132
-8
lines changed

src/publics/js/dev/page/editor/socket.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ export default class EditorSocket{
5454
}
5555

5656
send(name, data={}) {
57-
if(Config.isDebug() && Array.isArray(data)){
58-
for(const d of data){
59-
Debug.debug('SEND to \'' + name + '\': ', d);
60-
}
61-
}
6257
this.ws.send({
6358
event: name,
6459
room: this.doc_id,

src/publics/js/dev/utils/stack.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,75 @@ export default class Stack{
3535
return this.stack[n];
3636
}
3737
}
38+
39+
export class WaitingStack{
40+
/**
41+
* Init waitingStack object
42+
* @param size
43+
*/
44+
constructor(size) {
45+
this.size = size;
46+
this.stack = {};
47+
this.old = new Stack(size);
48+
}
49+
50+
/**
51+
* Push a new element
52+
* @param {string} uuid
53+
* @param {*} data
54+
*/
55+
push(uuid, data){
56+
this.stack[uuid] = data;
57+
}
58+
59+
/**
60+
* Get the element with the given uuid
61+
* @param {string} uuid
62+
* @return {*}
63+
*/
64+
get(uuid){
65+
return this.stack[uuid];
66+
}
67+
68+
/**
69+
* Append a new element in the stack
70+
* @param {string} uuid
71+
* @param {*} data
72+
*/
73+
add(uuid, data){
74+
this.push(uuid, {
75+
send: Date.now(),
76+
data: data,
77+
})
78+
}
79+
80+
/**
81+
* Archive an element
82+
* @param {string} uuid
83+
* @param {number|null} server_time
84+
* @param {number|null} time
85+
*/
86+
archive(uuid, server_time, time=null){
87+
const content = this.stack[uuid];
88+
delete this.stack[uuid];
89+
content['server'] = server_time;
90+
content['received'] = time ? time: Date.now();
91+
this.old.push(content);
92+
}
93+
94+
/**
95+
* Get all elements in stack
96+
* @return {{}}
97+
*/
98+
getAll(){
99+
return this.stack;
100+
}
101+
102+
/**
103+
* Return the size of current stack
104+
* @return {*}
105+
*/
106+
getSize(){
107+
return Object.keys(this.stack).length;
108+
}
109+
}

src/publics/js/dev/utils/websocket/socket.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* @date Last modification on 16/11/2020
66
* @version 1.0.0
77
*/
8+
import temporaryCardAlert from "/js/dev/component/text-alert.js";
89
import Debug from "/js/dev/utils/debugging/debug.js";
910
import EventManager from "/js/dev/utils/events.js";
11+
import Random from "/js/dev/utils/random.js";
12+
import Stack, {WaitingStack} from "/js/dev/utils/stack.js";
1013

1114
export default class Socket{
1215
constructor(options) {
@@ -21,6 +24,23 @@ export default class Socket{
2124
this.ws = new WebSocket(this.uri().href);
2225

2326
this.ws.onmessage = this.onMessage;
27+
28+
/**
29+
* WaitingStack for sockets ; Check if connected is still alive
30+
*/
31+
this.waitingStack = new WaitingStack(120);
32+
document.addEventListener('socket.confirm', evt => {this.confirm(evt.detail);})
33+
34+
setInterval(() => {
35+
const amount = this.waitingStack.getSize();
36+
if(amount > 20){
37+
temporaryCardAlert('Connexion', 'It seems than you are disconnected', 2000, '#ff501e')
38+
}else if(amount > 5){
39+
temporaryCardAlert('Connexion', 'It seems than you are disconnected', 2000, '#ff9000')
40+
}
41+
}, 1000);
42+
43+
2444
}
2545

2646
/**
@@ -64,6 +84,16 @@ export default class Socket{
6484
* @param {{}} content
6585
*/
6686
send(content){
87+
if(content.event !== 'join'){
88+
const uuid = Random.string(9);
89+
90+
this.waitingStack.add(uuid, content);
91+
92+
Debug.debug(`SEND ${uuid} to '${content.event}': `, content);
93+
94+
content['uuid'] = uuid;
95+
}
96+
6797
this.ws.send(JSON.stringify(content));
6898
}
6999

@@ -75,12 +105,32 @@ export default class Socket{
75105
try{
76106
const data = JSON.parse(e.data);
77107
Debug.debug('RECEIVE PACKET', data);
78-
if(data.event && data.data) EventManager.triggerCustom(`socket.receive.${data.event}`, data.data);
79-
else {
108+
if(data.event && data.data) {
109+
EventManager.triggerCustom(`socket.receive.${data.event}`, data.data);
110+
}else if(data.code && data.uuid && data.time){
111+
EventManager.triggerCustom(`socket.confirm`, data);
112+
} else {
80113
Debug.error('This packet hasn\'t valid event and data.', data);
81114
}
82115
}catch (error){
116+
Debug.debug(error);
83117
Debug.error('This packet can\'t be parsed as JSON.', e);
84118
}
85119
}
120+
121+
/**
122+
* When a confirm message is received
123+
* @param {{}} data
124+
*/
125+
confirm(data){
126+
const code = data.code;
127+
const uuid = data.uuid;
128+
const time = data.time;
129+
130+
if(code !== 'OK'){
131+
Debug.warn(`${uuid} come back with a non OK code.`)
132+
}
133+
134+
this.waitingStack.archive(uuid, time);
135+
}
86136
}

src/socket/socket.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ module.exports = function (wss) {
4747

4848
socket.on('message', async data => {
4949
data = JSON.parse(data);
50+
if(!('uuid' in data)) data['uuid'] = 'None';
5051
switch (data.event) {
5152
case 'update':
5253
try {
5354
Object.entries(rooms[data.room]).forEach(([, sock]) => {
54-
sock.send(JSON.stringify(data));
55+
if(sock === socket) {
56+
sock.send(JSON.stringify({
57+
code: "OK",
58+
uuid: data['uuid'],
59+
time: Date.now(),
60+
}));
61+
}else sock.send(JSON.stringify(data));
5562
});
5663
if (Object.entries(data.data).filter(([,el]) => el.type !== 'cursor-moves').length > 0) {
5764
let documentContent = (await db.getDocument(data["room"])).content;

0 commit comments

Comments
 (0)