Nodej将用户加入自定义房间

时间:2016-09-20 09:22:49

标签: javascript node.js

你想我有3个房间,如room1,room2和room3

我想加入一些用户到这个房间,我该怎么做?

我的代码不正确,我不能这样做

var socket      = require('socket.io'),
    express     = require('express'),
    fs          = require('fs'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = socket.listen(server),
    port        = process.env.PORT || 3000,
    redis       = require("redis"),
    redisClient = redis.createClient(),
    forever     = require('forever'),
    log         = require('log4node');

var io_redis = require('socket.io-redis');
io.adapter(io_redis({host: 'localhost', port: 6379}));

require('sticky-socket-cluster/replace-console')();

var options = {
    workers     : require('os').cpus().length,
    first_port  : 8000,
    proxy_port  : 3000,
    session_hash: function (req, res) {
        return req.connection.remoteAddress;
    },

    no_sockets: false
};
var room    = ["room1","room2","room3"];
require('sticky-socket-cluster')(options, start);

function start(port) {
    io.sockets.on('connection', function (socket) {
        socket.on('room1', function (room) {
            socket.join(room[0])
        });

        socket.on('room2', function (room) {
            socket.join(room[1])
        });    
    });
    server.listen(port, function () {
        console.log('Express and socket.io listening on port ' + port);
    });
}

当我使用此代码将用户加入时:

socket.on('room1', function (room) {
    socket.join(room[0])
});

我没有结果,代码不起作用,我不知道我该怎么做

0 个答案:

没有答案