简体   繁体   English

Ruby:TCPServer的连接超时检测

[英]Ruby: connection timeout detection for a TCPServer

been trying to understand how to implement a timeout detection to a ruby TCP server of mine. 一直试图了解如何对我的红宝石TCP服务器实施超时检测。 Mainly because sometimes clients with instable internet lose connection and i need my server to detect it. 主要是因为有时Internet不稳定的客户端会失去连接,而我需要我的服务器才能检测到它。

The idea is to teach my server to detect when a connection had been silent for longer than 30 seconds and abort it. 这个想法是要教我的服务器检测连接何时处于静默状态超过30秒并中止连接。 I've been trying to use timeout, but it terminates the program, so i need to use something like a simple timer that will just return an integer of seconds passed since the activation of the said timer. 我一直在尝试使用超时,但是它终止了程序,因此我需要使用简单的计时器之类的东西,它只会返回自激活所述计时器以来经过的秒数。

Is there an already made solution for that? 是否已经有解决方案? Sorry if it is a stupid question, it's just that googling it led me nowhere. 很抱歉,如果这是一个愚蠢的问题,只是因为谷歌搜索使我无处可去。 ps: using ruby 1.8 here. ps:在这里使用ruby 1.8。

The 'Time' object can report the number of seconds past by comparing it to previously created instances. “时间”对象可以通过将其与先前创建的实例进行比较来报告过去的秒数。 Consider: 考虑:

require 'time'

t0 = Time.now
sleep(2)
t1 = Time.now
t1.to_f - t0.to_f # => 2.00059294700623

So by creating a "last transmission" time object then checking its difference from "now" you can determine the number of seconds passed and act accordingly. 因此,通过创建“上次发送”时间对象,然后检查其与“现在”的时间差,您可以确定经过的秒数并采取相应的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM