Commits

Jeffrey Gelens committed 4974e04

Fixed #41, on_close was not called when connection is lost

  • Participants
  • Parent commits 05af297

Comments (0)

Files changed (5)

File examples/plot_graph.py

             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")])

File examples/wamp_example.py

         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):

File geventwebsocket/protocols/base.py

     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):

File geventwebsocket/protocols/wamp.py

         else:
             raise Exception("Unknown call")
 
-    def on_close(self):
-        self.app.on_close()

File geventwebsocket/websocket.py

 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.
         return True
 
     @property
+    def current_app(self):
+        return self.handler.server.application.current_app
+
+    @property
     def origin(self):
         if not self.environ:
             return
         """
 
         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()
         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)
 
         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=''):
         """
         """
 
         if self.closed:
-            raise WebSocketError("Connection is already closed")
+            self.current_app.on_close(MSG_ALREADY_CLOSED)
 
         try:
             message = self._encode_bytes(message)
 
             self.environ = None
 
+            self.current_app.on_close("Connection closed")
+
 
 class Stream(object):
     """