代码拉取完成,页面将自动刷新
var app = require('http').createServer(handle);
var io = require('socket.io').listen(app);
var Snake = require('./utils/Snake');
var Grid = require('./utils/Grid');
var fs = require('fs');
var url = require('url');
var port = 3100;
app.listen(port);
function handle(req, res) {
var path = '';
if (req.url === '/') {
path = __dirname + '/view/index.html';
} else {
path = __dirname + url.parse(req.url).pathname;
}
fs.readFile(path,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading ' + path);
}
res.writeHead(200);
res.end(data);
});
}
var grid = new Grid(40, function () {
io.sockets.emit('message', this);
this.deadSnakes().forEach(function (snake) {
io.sockets.emit('tip', snake.id + ' 挂了');
});
this.revivedSnakes().forEach(function (snake) {
snake.changeRevive();
io.sockets.emit('tip', snake.id + ' 复活了');
});
});
io.on('connection', function (socket) {
console.log("Connection " + socket.id + ".");
grid.push(socket.id);
socket.broadcast.emit('tip', 'Welcome ' + socket.id);
socket.on('message', function (msg) {
//io.sockets.emit('message', msg);
//socket.broadcast.emit('message',msg);
//socket.emit('message', msg);
grid.receiveMsg(socket.id, msg);
});
socket.on('disconnect', function () {
console.log("Connection " + socket.id + " terminated.");
grid.killSnake(socket.id);
});
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。