@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
46 lines (45 loc) • 1.95 kB
JavaScript
;
/*
Initialize a TCP socket connection to a project.
This mainly involves setting up the socket to so we we can send and receive
message over it.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const heartbeat_1 = __importDefault(require("./heartbeat"));
const handle_blob_1 = __importDefault(require("./handle-blob"));
const handle_message_1 = __importDefault(require("./handle-message"));
const logger_1 = __importDefault(require("@cocalc/backend/logger"));
const logger = (0, logger_1.default)("project-connection:initialize");
const enable_messaging_protocol_1 = __importDefault(require("@cocalc/backend/tcp/enable-messaging-protocol"));
function initialize(project_id, socket) {
logger.info("initializing socket");
(0, enable_messaging_protocol_1.default)(socket, "connection_to_a_local_hub");
socket.on("mesg", (type, mesg) => {
switch (type) {
case "blob":
(0, handle_blob_1.default)({
socket,
project_id,
uuid: mesg.uuid,
blob: mesg.blob,
ttlSeconds: mesg.ttlSeconds,
});
return;
case "json":
(0, handle_message_1.default)({ socket, project_id, mesg });
return;
default:
logger.warn("WARNING: unknown message type", type);
}
});
// Send a hello message to the project. I'm not sure if this is used for anything at all,
// but it is nice to see in the logs.
socket.write_mesg("json", { event: "hello" });
// start sending heartbeats over this socket, so project knows it is working.
(0, heartbeat_1.default)(socket);
}
exports.default = initialize;
//# sourceMappingURL=initialize.js.map