我有一个自定义设备,实现了TCP/IP协议栈,每5秒向远程IP发送一个字节。
在该远程IP上,我正在使用Rails 3.1.3构建一个站点,需要接收、存储和显示自定义设备发送的数据。
我考虑在后台运行一个TCP Socket,类似于this,但我不知道如何将其与Rails网站集成。应该放在哪里,如何启动它以及如何将数据传播到视图中。
是否有人知道我应该如何进行?
我有一个自定义设备,实现了TCP/IP协议栈,每5秒向远程IP发送一个字节。
在该远程IP上,我正在使用Rails 3.1.3构建一个站点,需要接收、存储和显示自定义设备发送的数据。
我考虑在后台运行一个TCP Socket,类似于this,但我不知道如何将其与Rails网站集成。应该放在哪里,如何启动它以及如何将数据传播到视图中。
是否有人知道我应该如何进行?
@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