我一直在构建协作绘图画布,我认为显示连接的用户及其所选颜色会很酷。我不太确定从哪里开始。
我试图遵循文档,但是我无法使其正常工作。
server.js
// jshint esversion: 6
var express = require('express');
var app = express();
var server = app.listen(process.env.PORT || 3000);
var usersCurrentMouseData = [];
var handshake;
app.use(express.static('public'));
console.log("My socket server is running");
var socket = require('socket.io');
var io = socket(server);
io.sockets.on('connection', newConnection);
function newConnection(socket) {
//let handshake = socket.handshake;
console.log('new connection: ' + socket.id);
//console.log(handshake);
socket.on('mouse', mouseMsg);
for (var data of usersCurrentMouseData) {
socket.emit('mouse', data);
}
socket.on('clear', function(data) {
usersCurrentMouseData.length = 0;
console.log(usersCurrentMouseData.length);
io.sockets.emit('browserReload');
console.log('sending reload');
});
function mouseMsg(data) {
socket.broadcast.emit('mouse', data);
usersCurrentMouseData.push(data);
console.log(data);
}
}
期望的结果是该系统能够显示被侵权的用户,但是我无法使其正常工作。