Receive TCP / IP data in rails application

I have a user device with a TCP / IP stack that sends bytes every 5 seconds to a remote IP address.

On this remote IP, I create a site with rails 3.1.3, which will have to receive, store and display the data sent by the custom device.

I was thinking that the TCP socket is running in the background, something like this , but I have no clue on how to integrate this with the rails site. Where to place it, how to run it and how to distribute data in views.

Does anyone know how I will act?

+4
source share
1 answer

To solve this problem, I created raketask that starts a TCP server that will process messages.

Note. This code has more than a year, so I'm not 100% sure how it behaves, but I think the main part is this:

@server = TCPServer.new(@host, port) loop do Thread.start(@server.accept) do |tcpSocket| port, ip = Socket.unpack_sockaddr_in(tcpSocket.getpeername) begin loop do line = tcpSocket.recv(100).strip # Read lines from the socket handle_message line # method to handle messages from the socket end rescue SystemCallError #close the sockets logic end end end 
+1
source

Source: https://habr.com/ru/post/1391669/


All Articles