为什么这个非常基本的风帆插座不起作用?

时间:2016-03-09 03:43:34

标签: javascript sockets sails.js

只是想知道为什么以下基本Web套接字无法正常工作?

io.socket.on('user', function(event){
    console.log("RECIEVED EVENT:",event);
})

sails.io.js包含在我的索引中,上面的代码位于test.js文件中,该文件位于assets / js下。我希望每次我向用户api发出任何请求时,我都会看到一个日志。哦,是的,用户api确实存在。我阅读了文档,但没有看到我在哪里出错。

1 个答案:

答案 0 :(得分:0)

原来你需要通过io.socket.get

注册活动
// The automatically-created socket is exposed as io.socket.
// Use .on() to subscribe to the 'user' event on the client.
// This event is sent by the Sails "create", "update",
// "delete", "add" and "remove" blueprints to any socket that
// is subscribed to one or more User model instances.
io.socket.on('user', function gotHelloMessage (data) {
  console.log('User alert!', data);
});
// Using .get('/user') will retrieve a list of current User models,
// subscribe this socket to those models, AND subscribe this socket
// to notifications about new User models when they are created.
io.socket.get('/user', function gotResponse(body, response) {
  console.log('Current users: ', body);
})