gpt4 book ai didi

javascript - 如何使用socket.io在多个模块中定义监听器

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:10 24 4
gpt4 key购买 nike

半年前,我用node.js 和socket.io 编写了一个小型多人游戏。现在我正在尝试改进它,我的改进之一是将代码分解为模块(顺便说一句。我使用的是expressjs 4.4和socket.io 1.0)。我有一个玩家队列,用于处理连接玩家的顺序。一切都很顺利,直到我陷入了 socket.io 的困境:我想处理玩家队列中的(断开)连接监听器,同时我想将所有与游戏相关的监听器放入我的游戏模块中。我尝试了一些事情,包括 Using socket.io from a moduleWhat's the correct way to pass a Socket.IO socket between modules?我没有带去上类。目前我正在传递 io 对象:

io = require("socket.io")(server);

我错过了什么吗?

我的代码:

PlayerManager.js

var log = require("npmlog"),
Player = require("./Model/Player").Player;

var playerIo;

var PlayerManager = (function() {
var self;

function PlayerManager(eventEmitter, io) {
log.info("PlayerManager", "loading PlayerManager");

self = this;

this.eventEmitter = eventEmitter;
this.io = io;
this.playerQueue = [];
this.players = [];

this.setSocketListeners();
}

PlayerManager.prototype.setSocketListeners = function() {
log.info("PlayerManager", "loading SocketListeners");

playerIo = this.io.of("/player").on("connection", function(socket) {
log.info("PlayerManager", "Player " + socket.id + " has connected");

socket.on("disconnect", function() {
log.info("PlayerManager", "Player " + socket.id + " has disconnected.");

self.removePlayer(socket.id);
});

self.addPlayer(new Player(socket));
});
};

PlayerManager.prototype.addPlayer = function(player) {
log.info("PlayerManager", "Pushing Player " + player.socket.id + " to Queue. Current position: " + this.playerQueue.length);

this.playerQueue.push(player);
};

PlayerManager.prototype.removePlayer = function(id) {
log.info("PlayerManager", "Removing Player " + id + " from Queue");

var player = this.getPlayerById(id);

if (!player) {
return;
}

this.playerQueue.splice(this.playerQueue.indexOf(player), 1);
};

PlayerManager.prototype.getPlayers = function() {
log.info("PlayerManager", "Choosing players (length of queue: " + this.playerQueue.length + ")");

this.players = [];

if (this.playerQueue.length >= 1) { //should be changed to 2 in PROD
var player;

for (var i = 0; i < 4; ++i) {
player = this.playerQueue.shift();

if (player !== undefined && !player.socket.disconnected) {
player.socket.join("players");
this.players.push(player);
}
}

return this.players;
} else {
return false;
}
};

PlayerManager.prototype.onGameFinished = function() {
var player;

while (this.players && this.players.length > 0) {
player = this.players.shift();

player.socket.leave("players");

log.info("GameManager", "players size: " + self.players.length);
}
};

PlayerManager.prototype.getPlayerById = function(id) {
for (var i = 0; i < this.playerQueue.length; ++i) {
if (this.playerQueue[i].socket.id === id) {
return this.playerQueue[i];
}
}

return false;
};

return PlayerManager;
})();

exports = module.exports = PlayerManager;

Game.js

var log = require("npmlog");
var config = require("./config");
var Duck = require("./Model/Duck").Duck;

var Game = (function() {
var self;

function Game(eventEmitter, io, players) {
log.info("Game", "loading ...");

self = this;

this.io = io;
this.eventEmitter = eventEmitter;
this.players = players;

setInterval(function() {
self.io.to("players").emit("blub");
});

// ...

return Game;
})();

exports = module.exports = Game;

为了完整性:

index.js(客户端)

define(["socketio"], function(io) {
var socket = io("/player");

socket.on("blub", function() {
console.log("blub");
});

setInterval(function() {
socket.emit("blub");
}, 1000);
});

最佳答案

一年后,是时候清理并分享我在那段时间获得的见解了。

将与 socket.io 相关的代码分布在整个项目中是糟糕的设计(或来自任何其他库的代码)。如果您需要更改包装器库,则必须在代码库中的许多地方执行此操作。更好的方法是将 socket.io 的使用限制在一个薄层,所有与 Websocket 之间的通信都在该薄层上完成。从那里开始,您将使用另一层消息传递:您自己的。 Node.js 自己的 EventEmitter 是您完成这项工作所需的一切。

关于javascript - 如何使用socket.io在多个模块中定义监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com