Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep websocket connection alive by sending pings when there is no activity #27

Merged
merged 3 commits into from
Feb 6, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion terminado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,42 @@ def _cast_unicode(s):

class TermSocket(tornado.websocket.WebSocketHandler):
"""Handler for a terminal websocket"""
def initialize(self, term_manager):
def initialize(self, term_manager, keep_alive_time=0):
"""
keep_alive_time - if there is no activity for x seconds specified in keep_alive_time,
a websocket ping message will be sent. Default is 0. Set it to 0 to
disable sending the ping message. This feature is used to keep proxies
such as nginx from automatically closing the connection when there is
no activity.
"""
self.term_manager = term_manager
self.term_name = ""
self.size = (None, None)
self.terminal = None

self._logger = logging.getLogger(__name__)

self.keep_alive_time = keep_alive_time #send a ping if there is no activity for more than this many of seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting time - I reckon this line is quite a lot longer than 80 characters. Can you put the comment on a separate line above?

self.last_activity_time = 0

def __timer(self):
if self.ws_connection is not None:
io_loop = self.ws_connection.stream.io_loop
t = io_loop.time()
if t - self.last_activity_time > self.keep_alive_time:
self.ping(b"ping")
#logging.debug('websocket ping')
self.last_activity_time = t
io_loop.add_timeout(1, self.__timer)

def __update_last_activity_time(self):
if self.ws_connection is not None:
io_loop = self.ws_connection.stream.io_loop
t = io_loop.time()
self.last_activity_time = t


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we trim the extra space between methods here?


def origin_check(self, origin=None):
"""Deprecated: backward-compat for terminado <= 0.5."""
return self.check_origin(origin or self.request.headers.get('Origin'))
Expand All @@ -77,15 +105,27 @@ def open(self, url_component=None):
self.terminal.clients.append(self)

self.send_json_message(["setup", {}])


if self.keep_alive_time>0:
self.__update_last_activity_time()
io_loop = self.ws_connection.stream.io_loop
io_loop.add_timeout(0.2*self.keep_alive_time, self.__timer)

self._logger.info("TermSocket.open: Opened %s", self.term_name)



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trim space again


def on_pty_read(self, text):
"""Data read from pty; send to frontend"""
self.send_json_message(['stdout', text])


def send_json_message(self, content):
json_msg = json.dumps(content)
self.write_message(json_msg)
self.__update_last_activity_time()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly for these, can they go inside an if self.keep_alive_time > 0 block?


def on_message(self, message):
"""Handle incoming websocket message
Expand All @@ -94,6 +134,7 @@ def on_message(self, message):
what kind of message this is. Data associated with the message follows.
"""
##logging.info("TermSocket.on_message: %s - (%s) %s", self.term_name, type(message), len(message) if isinstance(message, bytes) else message[:250])
self.__update_last_activity_time()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

command = json.loads(message)
msg_type = command[0]

Expand Down