diff --git a/index.js b/index.js index 789fa72..1741235 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,13 @@ -var app = require('express')(); +var express = require('express'); +var app = express(); var http = require('http').createServer(app); var io = require('socket.io')(http); - +var path = require('path'); var buffer = {}; +var users = {}; +var publicpath = path.join(__dirname, '/views'); +app.use('/', express.static(publicpath)); app.get('/', (req, res) => { res.sendFile(__dirname + '/views/index.html'); @@ -11,15 +15,17 @@ app.get('/', (req, res) => { io.on('connection', (socket) => { console.log('Nutzer verbunden!'); + userConnected(socket.id); + socket.on('disconnect', () => { - console.log('Nutzer verbindung unterbrochen') + 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; - console.log("first fill"); - } if (data.pos.x != buffer[data.id].x) { @@ -27,8 +33,27 @@ io.on('connection', (socket) => { buffer[data.id] = data.pos; } }); + + socket.on('nickname', function (data) { + socket.broadcast.emit('nickname_set', { name: data, id: socket.id }); + }); }); +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, () => { diff --git a/views/assets/js/client.js b/views/assets/js/client.js new file mode 100644 index 0000000..58fd693 --- /dev/null +++ b/views/assets/js/client.js @@ -0,0 +1,93 @@ +var socket = io(); + +socket.on('connect', function () { + console.log(socket.id); + +}); + +function getNickname() { + var nickname = document.getElementById('nickname').value; + console.log(nickname); + socket.emit('nickname', nickname); +} + + +document.addEventListener("DOMContentLoaded", function () { + var mouse = { + click: false, + move: false, + pos: { x: 0, y: 0 }, + }; + var canvas = document.getElementById('tracking'); + var width = window.innerWidth; + var height = window.innerHeight; + console.log("gg", height, width); + + //Canvas Size + canvas.width = width; + canvas.height = height; + + canvas.onmousemove = function (e) { + mouse.pos.x = e.clientX / width; + mouse.pos.y = e.clientY / height; + + }; + + function mainLoop() { + socket.emit('cursor_pos', { pos: mouse.pos, id: socket.id }); + + setTimeout(mainLoop, 25); + + } + + + socket.on('cursor_draw', function (data) { + var mouse_el = CursorElement(data.id); + mouse_el.style.left = (data.pos.x * width); + mouse_el.style.top = (data.pos.y * height); + + }); + + socket.on('nickname_set', function (data) { + console.log(data.name, data.id); + var elementId = 'cursor-' + data.id; + var el_nick = document.getElementById(elementId); + el_nick.innerHTML = "