Sails和Socket publishUpdate无法发送数据

时间:2015-08-03 15:53:14

标签: node.js sockets sails.js

我尝试学习Socket With sails但我遇到问题,publishUpdate无法发送数据。

让我展示我的代码

用户模块服务器端

'subscribe': function(req,res,next){
User.find(function(err,users){
    if (err) return next(err);
            User.watch(req.socket);
        User.subscribe(req.socket, users);
        console.log('now user is subscribe in ',req.socket.id); 
        res.send(200);
});
}

app.js客户端

(function(io){

// Connect To Socket
var socket  = io.sails.connect();

if(typeof console !== 'undefined'){
    console.log('Connecting to Sails.js ...');
}


socket.on('connect', function(){

  // is undefined
  console.log("This is handshake: ",socket.handshake)
   // is undefined
    console.log("This is from the connect: ", socket.id);
    //console.log(socket);


    // listen for message
    socket.on('message', function(message){
        console.log('New Message is Coming', message);
    });


    // Subscribe to the user model classroom and instance room
    io.socket.get('/user/subscribe',function(){
      //console.log('user now is subscribe ');
    });



    // log
    console.log(
        'Socket is now connected and globally accessible as `socket`.\n' + 
        'e.g. to send a GET request to Sails, try \n' + 
        '`socket.get("/", function (response) ' +
        '{ console.log(response); })`'
    );



});


})(

  // In case you're wrapping socket.io to prevent pollution of the global namespace,
  // you can replace `window.io` with your own `io` here:
  window.io

);
会话模块服务器端

        user.online = true;
        user.save(function(err,user){

            //req.isSocket
            var io = sails.io;

            // emit to all sockets (aka publish)
            // including yourself

            I try this and work great and I revived message
            //io.sockets.emit('message', {thisIs: 'USer Log in Id is ' + user.id});

            User.publishUpdate(user.id,{
                loggedIn : true,
                id : user.id
            });

            console.log('User is signin', user.id);
            // User Redirect To His Profile
            if (user.admin){
                res.redirect('/user/');
                return;
            }   

            res.redirect('/user/show/'+ user.id);

        });

现在我尝试重新加载服务器并在Chrome浏览器中显示

  

这是握手:未定义   app.js:14这是来自connect:undefined   app.js:32套接字现在已连接,可以socket全局访问。   例如向Sails发送GET请求,试试   socket.get("/", function (response) { console.log(response); })

我删除了publishUpdate并尝试发出。 我尝试了这个并且工作得非常好,我恢复了信息。

//io.sockets.emit('message', {thisIs: 'USer Log in Id is ' + user.id});

0 个答案:

没有答案