Rewrite reconnection logic

Implement proper session tracking and resume on reconnect for the
WebSocket server.
This commit is contained in:
Hornwitser committed 2020-04-02 21:58:44 +02:00
1 parent 026b7f1306
commit d4e072f376
24 files changed
+1403 -319

No files matched your search

+58 -40
View File
@@ -4,27 +4,9 @@ const ajv = new (require('ajv'))({
extendRefs: "fail",
strictDefaluts: true,
strictKeywords: true,
})
});
const closeSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
title: "Close Message",
description: "Format for a close messages on a link",
properties: {
"type": { const: "close" },
"data": {
additionalProperties: false,
required: ["reason"],
properties: {
"reason": { type: "string" },
}
},
},
}
const close = ajv.compile(closeSchema);
/**
* The base format for any message sent over the link
*/
@@ -36,28 +18,34 @@ const messageSchema = {
additionalProperties: false,
required: ["seq", "type", "data"],
properties: {
"seq": { type: "integer" },
"seq": { type: ["integer", "null"] },
"type": { type: "string" },
"data": { type: "object" },
},
}
};
const message = ajv.compile(messageSchema);
const serverActions = [
{},
];
const actionResponse = {
const heartbeat = ajv.compile({
$schema: "http://json-schema.org/draft-07/schema#",
title: "Heartbeat",
description: "Heartbeat sent over link",
type: "object",
additionalProperties: false,
required: ["action", "id", "status", "message"],
required: ["seq", "type", "data"],
properties: {
"action": { enum: ["TODO"] },
"id": { type: "integer" },
"status": { enum: ["started", "done", "error"] },
"message": { type: "string" },
"seq": { const: null },
"type": { const: "heartbeat" },
"data": {
type: "object",
additionalProperties: false,
required: ["seq"],
properties: {
"seq": { type: ["integer", "null"] },
},
},
},
};
});
const serverHandshake = ajv.compile({
$schema: "http://json-schema.org/draft-07/schema#",
@@ -82,15 +70,33 @@ const serverHandshake = ajv.compile({
"type": { const: "ready" },
"data": {
additionalProperties: false,
/* XXX anything here?
required: ["version"],
required: ["session_token", "heartbeat_interval"],
properties: {
"version": { type: "string" },
}*/
"session_token": { type: "string" },
"heartbeat_interval": { type: "number" },
},
},
},
},
closeSchema,
{
properties: {
"type": { const: "continue" },
"data": {
additionalProperties: false,
required: ["last_seq", "heartbeat_interval"],
properties: {
"last_seq": { type: ["integer", "null"] },
"heartbeat_interval": { type: "number" },
},
},
},
},
{
properties: {
"type": { const: "invalidate" },
"data": { const: {} },
},
},
],
});
@@ -130,7 +136,19 @@ const clientHandshake = ajv.compile({
},
},
},
closeSchema,
{
properties: {
"type": { const: "resume" },
"data": {
additionalProperties: false,
required: ["session_token", "last_seq"],
properties: {
"session_token": { type: "string" },
"last_seq": { type: ["integer", "null"] },
},
},
},
},
],
});
@@ -139,8 +157,8 @@ module.exports = {
compile: ajv.compile.bind(ajv),
message,
close,
heartbeat,
serverHandshake,
clientHandshake,
}
};