UNPKG

nomadder-client

Version:

A web based nomadic data sharing framework between non-internet connected servers

119 lines (118 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var nomadder_event_model_1 = require("./models/nomadder-event.model"); var ws; var NOMADDER_DATA = "NOMADDER-DATA"; var IDENTITY_KEY = "NOMADDER-IDENTITY"; function initializeSocket(url) { var location = window.location; var wsUri = null; if (location.protocol === "https:") { wsUri = "wss:"; } else { wsUri = "ws:"; } var hostname = url ? url : location.host; wsUri += "//" + hostname; return new WebSocket(wsUri); } function setupEventListener(ws, initialized) { ws.addEventListener("message", function (message) { console.log("Event", message); console.log("Event.data", message.data); // Parse message data var msg; try { msg = JSON.parse(message.data); } catch (_a) { // If it is not JSON it has nothing to do with this protocol return; } // Ensure right protocol if (msg.protocol !== nomadder_event_model_1.NOMADDER_PROTOCOL) { return; } // Verify correct event format if (!msg.protocolInformation.event) { return; } switch (msg.protocolInformation.event) { case nomadder_event_model_1.EventTypes.BATCH: handleBatchEvent(msg); break; case nomadder_event_model_1.EventTypes.IDENTITYSYNC: handleIdentitySyncEvent(msg); break; } }); ws.addEventListener("open", function (event) { // Initiate protocol here initialized(); }); } function initializeNomadderClient(url, initialized) { ws = initializeSocket(url); setupEventListener(ws, initialized); } exports.initializeNomadderClient = initializeNomadderClient; function handleBatchEvent(payload) { localStorage.setItem(NOMADDER_DATA, JSON.stringify(payload)); } function handleIdentitySyncEvent(payload) { localStorage.setItem(IDENTITY_KEY, JSON.stringify(payload)); } function sendSyncEvent() { var oldBatchEvent = localStorage.getItem(NOMADDER_DATA); if (oldBatchEvent) { var syncEvent = JSON.parse(oldBatchEvent); syncEvent.protocolInformation.event = nomadder_event_model_1.EventTypes.SYNC; ws.send(JSON.stringify(syncEvent)); } else { var emptySyncEvent = { "protocol": nomadder_event_model_1.NOMADDER_PROTOCOL, "hash": "", "protocolInformation": { "event": nomadder_event_model_1.EventTypes.SYNC, "payload": { "data": { "serverId": null, "data": [ // 5 ], "schemaDefinition": [ // 10 ] } } } }; ws.send(JSON.stringify(emptySyncEvent)); } } exports.sendSyncEvent = sendSyncEvent; function sendIdentityEvent(id, collection) { var identityEvent = { "protocol": nomadder_event_model_1.NOMADDER_PROTOCOL, "hash": "", "protocolInformation": { "event": nomadder_event_model_1.EventTypes.IDENTITY, "payload": { "id": id, "collection": collection } } }; ws.send(JSON.stringify(identityEvent)); } exports.sendIdentityEvent = sendIdentityEvent; function sendIdentitySyncEvent() { var oldIdentitySyncEvent = localStorage.getItem(IDENTITY_KEY); if (oldIdentitySyncEvent) { ws.send(oldIdentitySyncEvent); } else { console.error("You cannot call sendIdentitySyncEvent if you dont have any identity to sync in localstorage"); } } exports.sendIdentitySyncEvent = sendIdentitySyncEvent;