我不確定我瞭解套接字的服務器端配置。Socket.io回調函數參數混淆
var app = express();
var server = require('http').createServer(app);
var socketio = require('socket.io')(server, {
serveClient: config.env !== 'production',
path: '/socket.io-client'
});
這裏,代碼根據api引用創建一個與http服務器「連接」的套接字服務器。什麼是附加?
socketio.on('connection', function (socket) {
socket.on('create', function(room) {
console.log('joining a room');
socket.join(room);
console.log('socket joined room: ', room);
});
socket.address = socket.handshake.address !== null ?
socket.handshake.address.address + ':' + socket.handshake.address.port :
process.env.DOMAIN;
socket.connectedAt = new Date();
// Call onDisconnect.
socket.on('disconnect', function() {
onDisconnect(socket);
console.info('[%s] DISCONNECTED', socket.address);
});
// Call onConnect.
onConnect(socket);
console.info('[%s] CONNECTED', socket.address);
});
};
問題:在這裏,'套接字'變量是服務器還是客戶端?如果它是服務器的話,爲什麼socket.join(房間)工作? (客戶端被添加到一個房間)如果是客戶端,那麼爲什麼它必須聽'創建'事件(客戶端發出一個名爲create的事件來改變房間)。
來結束,我被三個'socket'在下面的代碼中。
socketio.on('connection', function (socket) {
socket.on('create', function(room) {
console.log('joining a room');
socket.join(room);
console.log('socket joined room: ', room);
});
});
非常感謝!你的解釋很棒! –