diff --git a/client/index.js b/client/index.js index 52603e6..3f2c724 100644 --- a/client/index.js +++ b/client/index.js @@ -3,5 +3,5 @@ import { io } from "socket.io-client"; const socket = io(); socket.on("update",(tank_info) => { - document.querySelector("#raw-info").innerHTML = tank_info; + document.querySelector("#raw-info").innerHTML = JSON.stringify(tank_info); }); \ No newline at end of file diff --git a/package.json b/package.json index 629e6e0..f597b97 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "description": "", "source": "client/index.html", + "entry": "src/main.js", "scripts": { "test": "mocha tests/" }, diff --git a/src/main.js b/src/main.js index 571f17e..88ca36f 100644 --- a/src/main.js +++ b/src/main.js @@ -5,17 +5,30 @@ import {Server} from 'socket.io'; import Express from 'express'; import http_server from 'http'; import path from 'path'; +import { fileURLToPath } from 'url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const app = Express(); const http = new http_server.Server(app); const io = new Server(http); +const connections = new Set(); -io.on("connection", (socket) => { +io.on("connection", socket => { + connections.add(socket); + console.log(`Connected client ${socket.id}.`); + socket.on('time-sync', callback => { + callback(performance.now()); + }); + + socket.on('disconnect', () => { + connections.delete(socket); + }); }); function main() { - app.use('/', Express.static(path.join('.','./dist'))); + app.use('/', Express.static(path.join(__dirname, '../dist'))); const server_port = parseInt(process.env.PORT) | 3000; http.listen(server_port, () => { @@ -24,12 +37,13 @@ function main() { process.on('SIGINT', () => { console.log('\nShutting server...'); - users.forEach(u => { - u.socket.disconnect(); - console.log(`Disconnected user ${u.name}.`); - }); + connections.forEach(socket => { + socket.disconnect(); + console.log(`Disconnected socket ${socket.id};`); + }) + http.close(() => { - console.log('Closed.'); + console.log('Terminated.'); process.exit(0); }); }); @@ -46,7 +60,7 @@ function main() { }, 1000/60); setInterval(() => { - io.emit("update",JSON.stringify(registry.get(tank, Motion))) + io.emit("update", registry.get(tank, Motion)) }, 50); }