javascript Barebones游戏服务器与移动跟踪和使用Node和Socket.io的房间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Barebones游戏服务器与移动跟踪和使用Node和Socket.io的房间相关的知识,希望对你有一定的参考价值。

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('/');
    socket.emit('rooms');
    socket.on('rooms', function (data) {
        console.log(data);
    });
    socket.on('move', function (data) {
        console.log("MOVE");
        console.log(data);
    });
    socket.on('oops', function (data) {
        console.log("ERROR");
        console.log(data);
    });
</script>
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var games = [];
var lookup = [];

server.listen(3000);

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
    //where the magic happens
    socket.emit('event', { hello: 'world' });
    socket.on('move', function (data) {
        console.log(games);
        try {
            if(games[lookup[socket.id]].client === socket.id) {
                //client can go
                if((games[lookup[socket.id]].moves % 2) === 1) {
                    games[lookup[socket.id]].moves++;
                    io.to(lookup[socket.id]).emit('move',"CLIENT MADE A MOVE");
                }
            } else if(games[lookup[socket.id]].host === socket.id) {
                //host can go
                if((games[lookup[socket.id]].moves % 2) === 0) {
                    games[lookup[socket.id]].moves++;
                    io.to(lookup[socket.id]).emit('move',"HOST MADE A MOVE");
                }
            } else {
                //something went wrong
                console.log("something went wrong");
            }
        } catch(err) {
            //the client is not in a room;
        }
    });
    socket.on('rooms', function (data) {
        //send the possible rooms by getting property names of io.sockets.adapter.rooms
        //searches property names for room_, and returns only those results
        var rooms = Object.getOwnPropertyNames(io.sockets.adapter.rooms).find(function(el) {
            return el.match('room_');
        });
        socket.emit('rooms', { data: rooms });
    });
    socket.on('host', function(data) {
        //becoming a host requested
        //global var game keeps track of client and moves
        games["room_" + socket.id] = {
            host: socket.id,
            client: false,
            moves: 0
        };
        //add the room to the lookup table for socket ids
        lookup[socket.id] = "room_" + socket.id;
        //put host in a room with the host's socket id
        socket.join("room_" + socket.id);
    });
    socket.on('join', function(data) {
        try {
            if(!games[data.game].client) {
                //set client to prevent others from joining
                games[data.game].client = socket.id;
                //add the room to the lookup table for socket ids
                lookup[socket.id] = data.game;
                socket.join(data.game);
            } else {
                socket.emit('oops', { message: 'room client already connected' });
            }
        } catch(err) {
            socket.emit('oops', { message: 'Game not found' });
        }
    });
});

以上是关于javascript Barebones游戏服务器与移动跟踪和使用Node和Socket.io的房间的主要内容,如果未能解决你的问题,请参考以下文章

html Barebones Bootstrap

html D3.js:Barebones十字准线

apache_conf Barebones Backbone路由器示例,用于具有本地URL的Apache站点,例如http:// local5 / backbone-router。

是否可以用 JavaScript 制作实时网络游戏

在 Cocos2d Javascript 游戏中将文件保存到服务器

从 PHP 获取信息到 HTML5 JavaScript 游戏