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

Why does this very basic sails socket not work?

本文关键字:插座 不起作用 非常 为什么      更新时间:2023-09-26

只是想知道为什么以下基本 Web 套接字不起作用?

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

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

事实证明,您需要通过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);
})