我目前正在使用Construct2进行游戏。 它是一个HTML5 Javascript引擎。 我可能在里面实现了clay.io。
我的问题是关于“房间” Clay.io也帮助了Rooms。但是我不确定我是否会接受这个提议。 https://clay.io/docs/rooms
因此,当我想将每场比赛的用户限制为10时。那我需要运行两台服务器吗?
socket.io服务器重新获取并返回数据..但是,每10个人运行两个游戏会不会混淆服务器数据?当服务器A上的人A射出某个1时,这个信息可能以某种方式结束在服务器B上的人B上? 或者指定的ID会以某种方式阻止这种情况吗?
以下是我想根据需要升级的示例服务器:
var entities = [], count = 0;
var io = require("socket.io").listen(8099);
var INITIAL_X = 5;
var INITIAL_Y = 5;
var INITIAL_VEL_X = 0;
var INITIAL_VEL_Y = 0;
io.set('log level', 1);
io.sockets.on("connection", function (socket) {
var myNumber = count++;
//assign number
var mySelf = entities[myNumber] = [myNumber, INITIAL_X, INITIAL_Y, INITIAL_VEL_X, INITIAL_VEL_Y];
//Send the initial position and ID to connecting player
console.log(myNumber + ' sent: ' + 'I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);
socket.send('I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);
//Send to conencting client the current state of all the other players
for (var entity_idx = 0; entity_idx < entities.length; entity_idx++) {
//send initial update
if (entity_idx != myNumber) {
entity = entities[entity_idx];
if (typeof (entity) != "undefined" && entity != null) {
console.log(myNumber + ' sent: C for ' + entity_idx);
socket.send('C,' + entity[0] + ',' + entity[1] + ',' + entity[2]);
//send the client that just connected the position of all the other clients
}
}
}
//create new entity in all clients
socket.broadcast.emit("message",
'C,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]);
socket.on("message", function (data) {
//if (myNumber == 0)
// console.log(myNumber + ' sent: ' +data);
var new_data = data.split(',');
if (new_data[0] == 'UM') {
mySelf[1] = new_data[1];
mySelf[2] = new_data[2];
mySelf[3] = new_data[3];
mySelf[4] = new_data[4];
//Update all the other clients about my update
socket.broadcast.emit("message",
'UM,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2] + ',' + mySelf[3] + ',' + mySelf[4]);
}
else if (new_data[0] == 'S') { // a s message
var shoot_info = [];
shoot_info[0] = new_data[1]; //ini x
shoot_info[1] = new_data[2]; //ini y
shoot_info[2] = new_data[3]; //degrees
//Update all the other clients about my update
socket.broadcast.emit("message",
'S,' + mySelf[0] + ',' + shoot_info[0] + ',' + shoot_info[1] + ',' + shoot_info[2]);
}
});
});
答案 0 :(得分:1)
Socket.io有可以限制广播的房间,请参阅:https://github.com/LearnBoost/socket.io/wiki/Rooms
然后您使用socket.broadcast.emit
io.sockets.in('roomname').emit
将此与Clay.io进行网格化的一种好方法是将房间名称设为room.id(在Construct 2插件中为RoomId表达式)。当Clay.io房间填满时(在C2中有条件),使用该唯一ID创建Socket.io房间,并将那个“房间填充”的玩家放在那个房间里。
我知道它有点不同,因为它是用CoffeeScript而不是Construct 2编写的游戏,但我们在Slime Volley游戏中使用Clay.io rooms + Socket.io房间。 Here is the code.