从 Sails.JS 发出 socket.io 事件并使用 Sails.JS 框架在服务器上处理它 [英] Emitting socket.io event from Sails.JS and handling it on server using Sails.JS skeleton

查看:45
本文介绍了从 Sails.JS 发出 socket.io 事件并使用 Sails.JS 框架在服务器上处理它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解从 Sails.js 客户端控制器发送 socket.io 事件并在服务器端控制器中处理它们的概念时遇到问题.首先我下载了​​使用npm的SailsJS项目骨架,结构如下:

I have a problem in understanding the concept of sending socket.io events from Sails.js client side controller and handling them in server-side controllers. First of all, I have downloaded the SailsJS project skeleton using npm and the structure as follows:

项目:

api
--adapters
--controllers
----server_userController.js
--models
--polices
--services

assets
--[styles, views, images, bower_components, and etc]
--js
----controllers
------userController.js
----app.js
----sails.io.js
----socket.io.js
config
node_modules
views

我在assets/js/controllers/server_userController.js"中写了以下内容:

I wrote the following in "assets/js/controllers/server_userController.js":

socket.emit('getUser', {
    _id: 10
});

现在,如何指定哪个服务器端控制器负责处理此事件,以及如何指定?我希望在api/controllers/server_userController.js"中接收此事件.所以,这样的东西将在控制器中:

Now, how to specify which server-side controller is responsible for handling this event, and how? I want this event to be received in "api/controllers/server_userController.js". So, something like this will be in the controller:

module.exports = {
  //Handling socket request here
  //Emitting socket request
};

提前致谢

推荐答案

如果您要做的只是通过套接字运行 Sails 控制器操作,那么您不需要发出任何东西根本.Sails 的部分魅力在于它可以通过套接字路由常规的、HTTP 样式的请求.所以如果你有一个控制器 /api/controllers/FooController.js 像:

If all you're trying to do is run a Sails controller action via sockets, then you don't need to emit anything at all. Part of Sails' charm is that it can route regular, HTTP-style requests over sockets. So if you have a controller /api/controllers/FooController.js like:

module.exports = {

    bar: function (req, res) {

        // Note, you can check whether this was a socket request
        // with req.isSocket, and if so access the connecting
        // socket with req.socket

        res.json({"message": "Hello!"});

    }

}

并且您在客户端应用程序中包含捆绑的 sails.io.js 文件,然后您可以通过以下方式访问该操作:

and you include the bundled sails.io.js file in your client-side app, then you can access that action via:

socket.get('/foo/bar', function(data) {
    // data will contain {message:"hello"}
}

注意:如果您使用的是 Sails v0.10.x 中包含的较新版本的 sails.io.js,它是 io.socket.get.

如果你真的想在服务器上捕获套接字事件,注册它们的最佳位置是在你的 /config/socket.js 文件中,在 onConnect 方法中:

If you really want to catch socket events on the server, the best place to register them is in your /config/socket.js file, in the onConnect method:

onConnect: function (socket, session) {

    socket.on('some_event', function(data) {
        // handle event here
    });

}

这比将事件处理程序放在控制器中要好,因为只要控制器操作运行,它就会再次绑定事件,并且您会看到回调被多次运行.

This is better than putting the event handlers in a controller, because any time that controller action ran it would bind the event again, and you'd see the callback being run multiple times.

这篇关于从 Sails.JS 发出 socket.io 事件并使用 Sails.JS 框架在服务器上处理它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆