var express = require('express'); var app = express(); var http = require('http').createServer(app); var io = require('socket.io')(http); var path = require('path'); var publicpath = path.join(__dirname, '/views'); app.use('/', express.static(publicpath)); app.get('/', (req, res) => { res.sendFile(__dirname + '/views/index.html'); }); var buffer = {}; var users = {}; var targetcount = 0; io.on('connection', (socket) => { console.log('Nutzer verbunden!'); userConnected(socket.id); socket.on('disconnect', () => { console.log(socket.id, 'Nutzer verbindung unterbrochen'); socket.broadcast.emit('cursor_remove', { id: socket.id }); userDisconnected(socket.id); }); socket.on('cursor_pos', function (data) { if (buffer[data.id] == null) { buffer[data.id] = data.pos; } if (data.pos.x != buffer[data.id].x) { socket.broadcast.emit('cursor_draw', { pos: data.pos, id: data.id }); buffer[data.id] = data.pos; } }); socket.on('nickname', function (data) { socket.broadcast.emit('nickname_set', { name: data, id: socket.id }); }); socket.on('remove_target', function (data) { socket.broadcast.emit('remove_target', data); }); socket.on('test', function () { createTarget(); }); function createTarget() { var pos = {}; pos.x = getRandomArbitrary(10, 720); pos.y = getRandomArbitrary(10, 720); function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } spawnTarget(pos); } function spawnTarget(pos) { targetcount++; socket.emit('create_target', { id: targetcount, pos: pos }); socket.broadcast.emit('create_target', { id: targetcount, pos: pos }); }; }); function userConnected(id) { users[id] = { Name: "unnamed", Points: 0 }; console.log("Add " + id); }; function userDisconnected(id) { delete users[id]; console.log("Remove " + id); }; http.listen(1234, () => { console.log("up and run on port 1234"); });