Skip to content

Commit

Permalink
Fixed #41, on_close was not called when connection is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
jgelens committed Nov 28, 2013
1 parent ee0ae33 commit da26a14
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions examples/plot_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def on_open(self):
self.ws.send("0 %s %s\n" % (i, random.random()))
gevent.sleep(0.1)

def on_close(self, reason):
print "Connection Closed!!!", reason


def static_wsgi_app(environ, start_response):
start_response("200 OK", [("Content-Type", "text/html")])
Expand Down
2 changes: 1 addition & 1 deletion examples/wamp_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def on_message(self, message):
print "message: ", message
super(WampApplication, self).on_message(message)

def on_close(self):
def on_close(self, reason):
print "closed"

def add(self, var1, var2):
Expand Down
4 changes: 2 additions & 2 deletions geventwebsocket/protocols/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def on_open(self):
def on_message(self, message):
self.app.on_message(message)

def on_close(self):
self.app.on_close()
def on_close(self, reason=None):
self.app.on_close(reason)

@property
def app(self):
Expand Down
2 changes: 0 additions & 2 deletions geventwebsocket/protocols/wamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,3 @@ def on_message(self, message):
else:
raise Exception("Unknown call")

def on_close(self):
self.app.on_close()
28 changes: 23 additions & 5 deletions geventwebsocket/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from .utf8validator import Utf8Validator


MSG_SOCKET_DEAD = "Socket is dead"
MSG_ALREADY_CLOSED = "Connection is already closed"


class WebSocket(object):
"""
Base class for supporting websocket operations.
Expand Down Expand Up @@ -101,6 +105,10 @@ def _is_valid_close_code(self, code):

return True

@property
def current_app(self):
return self.handler.server.application.current_app

@property
def origin(self):
if not self.environ:
Expand Down Expand Up @@ -280,7 +288,8 @@ def receive(self):
"""

if self.closed:
raise WebSocketError("Connection is already closed")
self.current_app.on_close(MSG_ALREADY_CLOSED)
raise WebSocketError(MSG_ALREADY_CLOSED)

try:
return self.read_message()
Expand All @@ -289,14 +298,17 @@ def receive(self):
except ProtocolError:
self.close(1002)
except error:
raise WebSocketError("Socket is dead")
self.current_app.on_close(MSG_SOCKET_DEAD)

return None

def send_frame(self, message, opcode):
"""
Send a frame over the websocket with message as its payload
"""
if self.closed:
raise WebSocketError("Connection is already closed")
self.current_app.on_close(MSG_ALREADY_CLOSED)
raise WebSocketError(MSG_ALREADY_CLOSED)

if opcode == self.OPCODE_TEXT:
message = self._encode_bytes(message)
Expand All @@ -319,7 +331,11 @@ def send(self, message, binary=None):

opcode = self.OPCODE_BINARY if binary else self.OPCODE_TEXT

self.send_frame(message, opcode)
try:
self.send_frame(message, opcode)
except WebSocketError:
self.current_app.on_close(MSG_SOCKET_DEAD)
raise WebSocketError(MSG_SOCKET_DEAD)

def close(self, code=1000, message=''):
"""
Expand All @@ -329,7 +345,7 @@ def close(self, code=1000, message=''):
"""

if self.closed:
raise WebSocketError("Connection is already closed")
self.current_app.on_close(MSG_ALREADY_CLOSED)

try:
message = self._encode_bytes(message)
Expand All @@ -351,6 +367,8 @@ def close(self, code=1000, message=''):

self.environ = None

self.current_app.on_close("Connection closed")


class Stream(object):
"""
Expand Down

0 comments on commit da26a14

Please sign in to comment.