I am writing a web application using Sinatra on an embedded device and need to be able to send and receive data over a Unix socket to the embedded code running on the same device. We will be most likely using JSON for the data format but I haven't found an example that really illustrates how to set this up and communicate with the web application.
我正在使用Sinatra在嵌入式设备上编写Web应用程序,并且需要能够通过Unix套接字将数据发送到同一设备上运行的嵌入式代码。我们很可能使用JSON作为数据格式,但我没有找到一个真正说明如何设置它并与Web应用程序通信的示例。
I'm looking for both an example of the code that will listen on the socket and also an example that shows how to get that data into the Sinatra web page. I've seen bits and pieces of both but nothing that ties the concepts together.
我正在寻找将在套接字上监听的代码示例,以及一个示例,说明如何将这些数据导入Sinatra网页。我已经看过两者的点点滴滴,但没有把概念联系在一起的东西。
2 个解决方案
#1
2
I haven't done it personally but I know Event Machine supports working with unix socket. The advantage of event machine is that you can have code listening to the socket without 'interfering' with sinatra web server. So you can for example listen to socket for some statistics, store those statistics in memory and then return them to clients on HTTP requests.
我没有亲自完成,但我知道Event Machine支持使用unix socket。事件机器的优点是你可以让代码监听套接字而不会干扰sinatra web服务器。因此,您可以例如监听套接字以获取某些统计信息,将这些统计信息存储在内存中,然后根据HTTP请求将它们返回给客户端。
And if you are going for EventMachine I suggest using thin as your webserver which is implemented on top of it so you don't need to start it manually.
如果您要使用EventMachine,我建议使用thin作为您的网络服务器,并在其上实现,因此您无需手动启动它。
#2
1
I've never done this personally, but Unicorn can listen on unix sockets, and can serve Sinatra apps.
我个人从来没有这样做,但是Unicorn可以在unix套接字上听,并且可以为Sinatra应用程序提供服务。
#1
2
I haven't done it personally but I know Event Machine supports working with unix socket. The advantage of event machine is that you can have code listening to the socket without 'interfering' with sinatra web server. So you can for example listen to socket for some statistics, store those statistics in memory and then return them to clients on HTTP requests.
我没有亲自完成,但我知道Event Machine支持使用unix socket。事件机器的优点是你可以让代码监听套接字而不会干扰sinatra web服务器。因此,您可以例如监听套接字以获取某些统计信息,将这些统计信息存储在内存中,然后根据HTTP请求将它们返回给客户端。
And if you are going for EventMachine I suggest using thin as your webserver which is implemented on top of it so you don't need to start it manually.
如果您要使用EventMachine,我建议使用thin作为您的网络服务器,并在其上实现,因此您无需手动启动它。
#2
1
I've never done this personally, but Unicorn can listen on unix sockets, and can serve Sinatra apps.
我个人从来没有这样做,但是Unicorn可以在unix套接字上听,并且可以为Sinatra应用程序提供服务。