如何运行socket.io和now.js没有任何问题?

时间:2014-03-12 12:18:33

标签: socket.io nowjs-sockets

我试图运行一个使用socket.io来实现某些功能的应用程序,而现在又运行其他一些功能?当我运行应用程序时,socket.io继续发生冲突。请建议我必须做什么?

聊天模块

var io = require('socket.io');
var users = {};
exports.initialize = function(server){
       io= io.listen(server,{ log: false });

        io.sockets.on('connection', function(socket) {


            socket.on('send message', function(data){
                io.sockets.in(socket.room).emit('message', socket.user, data);
            });



            socket.on('messageandsender', function(user){
                var rcvr = user.receiver;
                var sndr = user.sender; 
                io.sockets.emit('msgandreceiver',{'message': user.msg, 'rvr': rcvr, 'sndr': sndr});
            });         


            socket.on('disconnect', function(){
                    delete users[socket.user];
                io.sockets.emit('update',users);
                if(socket.user != undefined)
                socket.broadcast.emit('info', 'SERVER', socket.user + ' has gone offline');
                socket.leave(socket.room);
            });
        });
    };

绘制模块

var nowjs = require("now");
exports.startnow = function(server){
var everyone = nowjs.initialize(server);
nowjs.on("connect", function () {
    console.log("Joined: " + this.user.clientId);
nowjs.on("disconnect", function () {
    console.log("Left: " + this.user.clientId);
    nowjs.getClient(this.user.clientId,function(){
        console.log(this.now.name);
    });
    updateList();
});

everyone.now.distributeMessage = function (message) {
    everyone.now.processIncomingChatMessage(this.now.name, message);
};

everyone.now.distributePaintCommand = function (paintCommand) {
    everyone.exclude(this.user.clientId).now.processPaintCommand(paintCommand);
};

everyone.now.setName = function(){
    userlist.push(this.now.name);
    console.log(userlist);
    everyone.now.updateUserList(userlist);
};

var userlist = [];

updateList = function(){
    userlist = [];
    everyone.getUsers(function (users) {
        for (var i = 0; i < users.length; i++){
            nowjs.getClient(users[i],function(){
                console.log(this.now.name);
                userlist.push(this.now.name);
            });
        }
    });
};

everyone.now.getUserList = function() {
    this.now.updateUserList(userlist);
};

});

};

in server.js

var server = http.createServer(app).listen(app.get('port'), function(){
});
chat.initialize(server);
paint.startnow(server);

在这种情况下我添加了油漆模型油漆和聊天模块的套接字冲突多次发出以下连接警告。但无法连接但是当我评论一个聊天或绘画时它工作正常< /强>

WebSocket connection to 'ws://localhost:3000/socket.io/1/websocket/6gMr-tmuaA0kJnNQrIQz' failed: WebSocket is closed before the connection is established. socket.io.js:2438

1 个答案:

答案 0 :(得分:0)

你不能同时运行这两件事,因为两者都试图要求socket @ same port尝试nginx for node app