Rails 应用程序中的持久 TCP 连接 [英] Persistent TCP connection in Rails App
问题描述
我有一个在服务器上运行的第三方应用程序,它对客户端进行身份验证并返回响应.
I have a third party app running on a server, which authenticates the client and responses back.
我的问题是对于我到达 Rails 应用程序的每个请求,我需要建立 TCP 连接并获取数据.是否可以建立持久连接,以便我可以减少建立连接的开销.
My problem here is for each and every request that I get to my rails app I need to establish a TCP connection and get the data. Is it possible to have a persistent connection so that I can reduce overhead in establishing the connection.
推荐答案
我希望你正在使用 ruby 套接字.我认为你可以有一个方法来返回一个连接对象.您可以为连接对象创建一个类变量.方法将检查是否连接然后返回否则创建新的.
I hope you are using ruby socket. I think you can have a method which will return a connection object. You can have a class variable for connection object. method will check if connection then return otherwise create new one.
self.get_connection
return @@conObj if @@conObj
return @@conObj = TCPSocket::new( "192.168.1.1", 100 )
end
我不确定,但它可能对你有帮助.
I'm not sure but it might help you.
这篇关于Rails 应用程序中的持久 TCP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!