mirror of
https://github.com/demostf/sync.git
synced 2026-06-03 10:34:06 +02:00
send stop packets when the session owner disconnects
This commit is contained in:
parent
7dbb1e45cc
commit
b65cad5f19
2 changed files with 41 additions and 27 deletions
31
sync.js
31
sync.js
|
|
@ -1,5 +1,5 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.__esModule = true;
|
||||
var websocket_1 = require("websocket");
|
||||
var http_1 = require("http");
|
||||
var sessions = {};
|
||||
|
|
@ -18,6 +18,12 @@ var wsServer = new websocket_1.server({
|
|||
function originIsAllowed(origin) {
|
||||
return true;
|
||||
}
|
||||
function sendToSession(session, message) {
|
||||
for (var _i = 0, _a = session.clients; _i < _a.length; _i++) {
|
||||
var client = _a[_i];
|
||||
client.sendUTF(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
wsServer.on('request', function (request) {
|
||||
if (!originIsAllowed(request.origin)) {
|
||||
// Make sure we only accept requests from an allowed origin
|
||||
|
|
@ -55,25 +61,19 @@ wsServer.on('request', function (request) {
|
|||
case 'tick':
|
||||
if (sessions[data.session]) {
|
||||
sessions[data.session].tick = data.tick;
|
||||
for (var _i = 0, _a = sessions[data.session].clients; _i < _a.length; _i++) {
|
||||
var client = _a[_i];
|
||||
client.sendUTF(JSON.stringify({
|
||||
sendToSession(sessions[data.session], {
|
||||
type: 'tick',
|
||||
tick: data.tick
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'play':
|
||||
if (sessions[data.session]) {
|
||||
sessions[data.session].playing = data.play;
|
||||
for (var _b = 0, _c = sessions[data.session].clients; _b < _c.length; _b++) {
|
||||
var client = _c[_b];
|
||||
client.sendUTF(JSON.stringify({
|
||||
sendToSession(sessions[data.session], {
|
||||
type: 'play',
|
||||
play: data.play
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -88,6 +88,9 @@ wsServer.on('request', function (request) {
|
|||
session.clients.splice(index, 1);
|
||||
}
|
||||
if (session.owner === connection) {
|
||||
sendToSession(sessions[session.name], {
|
||||
type: 'stop'
|
||||
});
|
||||
sessions[session.name] = null;
|
||||
break;
|
||||
}
|
||||
|
|
@ -96,3 +99,9 @@ wsServer.on('request', function (request) {
|
|||
}
|
||||
});
|
||||
});
|
||||
process.on('SIGINT', function () {
|
||||
process.exit();
|
||||
});
|
||||
process.on('SIGTERM', function () {
|
||||
process.exit();
|
||||
});
|
||||
|
|
|
|||
21
sync.ts
21
sync.ts
|
|
@ -54,6 +54,12 @@ function originIsAllowed(origin) {
|
|||
return true;
|
||||
}
|
||||
|
||||
function sendToSession(session: Session, message) {
|
||||
for (const client of session.clients) {
|
||||
client.sendUTF(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
|
||||
wsServer.on('request', function (request) {
|
||||
if (!originIsAllowed(request.origin)) {
|
||||
// Make sure we only accept requests from an allowed origin
|
||||
|
|
@ -92,23 +98,19 @@ wsServer.on('request', function (request) {
|
|||
case 'tick':
|
||||
if (sessions[data.session]) {
|
||||
sessions[data.session].tick = data.tick;
|
||||
for (const client of sessions[data.session].clients) {
|
||||
client.sendUTF(JSON.stringify({
|
||||
sendToSession(sessions[data.session], {
|
||||
type: 'tick',
|
||||
tick: data.tick
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'play':
|
||||
if (sessions[data.session]) {
|
||||
sessions[data.session].playing = data.play;
|
||||
for (const client of sessions[data.session].clients) {
|
||||
client.sendUTF(JSON.stringify({
|
||||
sendToSession(sessions[data.session], {
|
||||
type: 'play',
|
||||
play: data.play
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,6 +125,9 @@ wsServer.on('request', function (request) {
|
|||
session.clients.splice(index, 1);
|
||||
}
|
||||
if (session.owner === connection) {
|
||||
sendToSession(sessions[session.name], {
|
||||
type: 'stop'
|
||||
});
|
||||
sessions[session.name] = null;
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue