1 Star 1 Fork 0

Asciphx / socketIo

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未指定开源许可证,未经作者的许可,此代码仅用于学习,不能用于其他用途。
克隆/下载
www 3.74 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
#!/usr/bin/env node
const s = require('node-static'), o = require('os'), h = require('http'),
socketIO = require('socket.io'), _ = require('underscore');
Array.prototype.remove = f => {const r = this.slice(f + 1 || this.length);
this.length = f < 0 ? this.length + f : f; return this.push.apply(this, r)}
let rooms = new Array(), userNum = 0;
const fileServer = new (s.Server)();
const app = h.createServer((req, res) => {
fileServer.serve(req, res);
}).listen(2013, () => { console.log(`Server running at http://127.0.0.1:2013/`); });
const io = socketIO.listen(app);
io.sockets.on('connection', socket => {
// if (io.sockets.connected[socket.id]) {
// _.findWhere(io.sockets.sockets, { id: socket.id }).emit('login', socket.id);
// }
socket.emit('login', socket.id);
socket.on('message', mes => {
console.log(mes)
switch (mes.d) {
case "all": mes.r ? io.to(mes.r).emit("message", mes) : io.emit("message", mes); break;
case "del": const player = _.findWhere(io.sockets.sockets, { id: mes.id });
player && (mes.r ? player.leave(mes.r) : player.disconnect(true)); break;
default: mes.r ? socket.broadcast.to(mes.r).emit('message', mes) : socket.broadcast.emit('message', mes);
}
});
socket.on('create or join', room => {
let num = io.nsps['/'].adapter.rooms[room];
if (num === undefined) {
socket.join(room); io.emit('created', room); rooms.push(room);
socket.emit('joined', socket.id, room);
} else if (num.length < 3) {
socket.join(room); socket.emit('joined', socket.id, room);
socket.emit('getRoomPlayers', Object.keys(num.sockets));
socket.broadcast.to(room).emit('playerCome', socket.id);
if (num.length === 3) io.sockets.in(room).emit('ready');
} else {
socket.emit('full', room);
}
socket.emit('log', '房间:' + room + '' + (num ? num.length : 1) + '个用户');
});
socket.on("leaveRoom", room => {
if (room === "") return;
io.to(room).emit("leaveRoom", room, socket.id);
socket.leave(room);
let num = io.nsps['/'].adapter.rooms[room];
num = (num ? num.length : 0);
if (num === 0) {
io.emit("destroyRoom", room);
rooms.remove(rooms.findIndex(v => v === room));
}
})
socket.on("getRooms", () => {
socket.emit("getRooms", rooms);
})
socket.on("getUsers", bool => {
let list = io.nsps['/'].adapter.rooms, users = [];
for (i in list) {
if (i.length === 20) users.push(i);
}; userNum = users.length;
if (bool) socket.broadcast.emit('userCome', socket.id, userNum)
socket.emit("getUsers", users, userNum);
})
socket.on('IPv4', () => {
var ifaces = o.networkInterfaces();
for (dev in ifaces) {
ifaces[dev].forEach(details => {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('IPv4', details.address);
}
});
}
});
socket.on("disconnect", () => {
for (i in rooms) {
if (typeof rooms[i] === 'function') break;
let room = io.nsps['/'].adapter.rooms[rooms[i]];
if (room === undefined) {
rooms.remove(rooms.findIndex(v => v === rooms[i]));
socket.broadcast.emit('exit', socket.id, true);
return;
} else {
socket.broadcast.to(rooms[i]).emit("leaveRoom", rooms[i], socket.id);
}
}
socket.broadcast.emit('exit', socket.id);
});
});

评论 ( 0 )

你可以在登录后,发表评论

JavaScript
1
https://gitee.com/ASCIPHX/socketIo.git
git@gitee.com:ASCIPHX/socketIo.git
ASCIPHX
socketIo
socketIo
master

搜索帮助