AAEO.io/index.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-07-28 21:33:48 +00:00
var express = require('express');
var app = express();
2019-07-28 01:26:48 +00:00
var http = require('http').createServer(app);
var io = require('socket.io')(http);
2019-07-28 21:33:48 +00:00
var path = require('path');
2019-07-28 01:26:48 +00:00
var buffer = {};
2019-07-28 21:33:48 +00:00
var users = {};
var publicpath = path.join(__dirname, '/views');
app.use('/', express.static(publicpath));
2019-07-28 01:26:48 +00:00
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
io.on('connection', (socket) => {
console.log('Nutzer verbunden!');
2019-07-28 21:33:48 +00:00
userConnected(socket.id);
2019-07-28 01:26:48 +00:00
socket.on('disconnect', () => {
2019-07-28 21:33:48 +00:00
console.log(socket.id, 'Nutzer verbindung unterbrochen');
socket.broadcast.emit('cursor_remove', { id: socket.id });
userDisconnected(socket.id);
2019-07-28 01:26:48 +00:00
});
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;
}
});
2019-07-28 21:33:48 +00:00
socket.on('nickname', function (data) {
socket.broadcast.emit('nickname_set', { name: data, id: socket.id });
});
2019-07-28 01:26:48 +00:00
});
2019-07-28 21:33:48 +00:00
function userConnected(id) {
users[id] = {
Name: "unnamed",
Points: 0
};
console.log("Add " + id);
};
function userDisconnected(id) {
delete users[id];
console.log("Remove " + id);
};
2019-07-28 01:26:48 +00:00
http.listen(1234, () => {
console.log("up and run on port 1234");
});