Ruby tcpserver 客户端 服务器

发布于 2024-10-24 19:16:44 字数 1048 浏览 3 评论 0原文

我有一个应用程序,我正在编码将日志记录信息通过 tcpsocket 发送到服务器,并让监视器客户端连接到服务器以查看日志记录数据。到目前为止,我已经能够到达将信息发送到服务器的阶段,但是我需要一些关于如何进行下一阶段的想法。使用 Ruby tcpsever 我可以使用什么方法让服务器将传入数据重新发送到客户端?如何跨线程存储数据?

require "socket"  

server_socket = TCPServer.new('localhost', 2200)  

loop do  
  # Create a new thread for each connection.
  Thread.start(server_socket.accept) do |session|

    # check if received is viewer request
    line = session.gets
    if line =~ /viewer/
      @filter = line[/\:(.*?)\:/]
      session.puts "Listining for #{filter}"

      loop do
        if (log = ### need input here from logging app ###)
          # Show if filter is set for all or cli matches to filter
          if @filter == ':all:' || log =~ /\:(.*?)\:/
            session.puts log
          end

          # Read trace_viewer input. 
          if session.gets =~ /quit/
            # close the connections
            session.puts "Closing connection. Bye!"
            session.close
            break
          end
        end
      end
    end
  end  
end

I have an application that I am coding to have the logging info be sent over tcpsocket to a server and have a monitor client connect to the server to view the logging data . So far i am able to get to the stage where the info is sent to the server however I need some thoughts on how to go about the next stage. Using Ruby tcpsever what methodologies can I use to have the server resend the incoming data to a client? How can I have data stored across threads?

require "socket"  

server_socket = TCPServer.new('localhost', 2200)  

loop do  
  # Create a new thread for each connection.
  Thread.start(server_socket.accept) do |session|

    # check if received is viewer request
    line = session.gets
    if line =~ /viewer/
      @filter = line[/\:(.*?)\:/]
      session.puts "Listining for #{filter}"

      loop do
        if (log = ### need input here from logging app ###)
          # Show if filter is set for all or cli matches to filter
          if @filter == ':all:' || log =~ /\:(.*?)\:/
            session.puts log
          end

          # Read trace_viewer input. 
          if session.gets =~ /quit/
            # close the connections
            session.puts "Closing connection. Bye!"
            session.close
            break
          end
        end
      end
    end
  end  
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-10-31 19:16:44

当客户端连接到服务器时,这听起来像是典型的客户端/服务器配置,其中一些客户端发送数据,而其他客户端则请求数据。您是否有任何理由不使用标准的 HTTP 客户端/服务器(即 Web 服务器),而不是重新发明轮子?使用 Sinatra 或 Padrino,甚至 Rails,您就基本完成了。

如何跨线程存储数据?

Ruby 的 Thread 模块包括 Queue,这对于在线程之间移动数据。文档页面有一个示例,应该会有所帮助。

使用队列的相同想法也适用于使用表。我建议使用数据库作为队列供您使用,而不是在内存中进行。如果它是内存队列并且客户端尚未检索所有数据,则断电或应用程序崩溃将丢失所有数据。写入和读取数据库意味着数据不会出现此类问题。

保持模式简单,为其提供合理的索引,并且对于大多数用途来说它应该足够快。您需要一些内务代码来保持数据库干净,但是使用 SQL 或像 Sequel 或 ActiveRecord 这样的 ORM 很容易做到这一点。

With clients connecting to the server it sounds like a typical client/server configuration, with some clients sending data, and others requesting it. Is there any reason you don't use a standard HTTP client/server, i.e., a web server, instead of reinventing the wheel? Use Sinatra or Padrino, or even Rails and you'll be mostly finished.

How can I have data stored across threads?

Ruby's Thread module includes Queue, which is good for moving data around between threads. The document page has an example which should help.

The same ideas for using a queue would apply to using tables. I'd recommend using a database to act as a queue for your use, rather than do it in memory. A power outage, or app crash will lose all the data if it's an in-memory queue and the clients haven't retrieved everything. Writing and reading a database means the data would survive such problems.

Keep the schema simple, provide a reasonable index on it, and it should be fast enough for most uses. You'll need some housekeeping code to keep the database clean, but that's easy using SQL, or an ORM like Sequel or ActiveRecord.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文