我试图将套接字对象提供给我的' ConnectionHandler' class,但是当使用这个套接字对象时,它会出现这样的错误:'无法读取未定义的socket.io'的属性套接字。
服务器类:
Server.prototype.handleConnections = function ()
{
this.queueTime = 15; // Queue time in seconds
var that = this; // Create a global variable of the server object
// On incoming connection
this.io.on('connection', function (socket) {
console.log('connection incoming...'); // Log a message to the server console
// When a client tries to join the queue
socket.on('client_join_queue', function (username) {
// Check if the username is valid
if (! (username.length < 3)) {
var newPlayer = new player(username);
var connectionHandler = new connectionHandling(socket, that, newPlayer);
that.connections.push(connectionHandler);
}
});
});
}
&#13;
ConnectionHandler类:
'use strict';
var ConnectionHandler = function (_socket, _server, _player)
{
this.socket = _socket;
this.server = _server;
this.player = _player;
this.server.queueHandler.addPlayer(this.player);
this.server.connections[0].socket.emit('player_joined_queue', this.player, this.server.queueHandler.getQueue().length);
var that = this;
socket.on('disconnect', function () {
console.log("user disconnected");
console.log("queue:", that.server.queueHandler.getQueue());
});
}
module.exports.ConnectionHandler = ConnectionHandler;
&#13;
我完全不知道我做错了什么。
答案 0 :(得分:0)
fljs在说什么,你发出一个名为'player_joined_queue'的事件。 但是您正在侦听事件'client_join_queue'。您需要使用您正在发出的相同名称来侦听事件。所以你需要改变一个或另一个。例如,
socket.on('player_joined_queue', function (username) {
...