var app = require('http').createServer()
var io = require('socket.io')(app);
var PORT = 3000;
//客户端计数
var clientCount = 0;
//存储客户端socket
var socketMap = {};
app.listen(PORT);
io.on('connection', function (socket) {
// 这个进不去,coonection 失败,连不上socket.io
clientCount += 1;
socket.clientNum = clientCount;
socketMap[clientCount] = socket;
console.log(clientCount);
if(clientCount % 2 == 1) {
socket.emit('wwaiting', 'waitiong for another person');
}else {
socket.emit('start');
socketMap[(clientCount - 1)].emit('start');
}
socket.on('disconnect', function() {
});
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log('websocket listening on port ' + PORT);