I figured out a solution, but it's not perfect so far.
I set a count down timer when each connection is established. It will do the following.
The timeout interval is set when you run gotty like gotty -w --timeout=60 <cmd>.
The left time is decreasing 1 every second since the connection is established. If user has no operation on the gotty web page, it will count down to 0 and will trigger a function named boom which will cause disconnection.
The left time will be set to init value if the user has typed anything on gotty web page. To say technically, function Rewind() will be called inside processReceive() and processSend(). The Rewind() function will set the left time to initial value(the one in gotty command line). The timer will count down again and do same thing in step 2.
It works like a old-fashioned stopwatch.
The issue is, when I run command like sleep 80 or start a long-run programme like top. It will disconnect when timeout. This bothers in some cases.
+1 I need timeout to free TCP port for further connections.
My case is multiple GoTTY instances running on one server in a specific port range, I'd like to use timeout to free used ports and server resources.