61 lines
No EOL
1.4 KiB
JavaScript
61 lines
No EOL
1.4 KiB
JavaScript
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');
|
|
});
|
|
|
|
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 });
|
|
});
|
|
});
|
|
|
|
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");
|
|
}); |