Jeffrey Gelens avatar Jeffrey Gelens committed 8bf976f

Added possibility to define a pre_start_hook

Comments (0)

Files changed (3)

geventwebsocket/handler.py

             self.websocket.close()
 
     def run_application(self):
-        self.logger.debug("Application started")
+        if self.server.pre_start_hook:
+            self.logger.debug("Calling pre-start hook")
+            self.server.pre_start_hook(self)
+
+        self.logger.debug("Initializing WebSocket")
         self.result = self.upgrade_websocket()
 
         if hasattr(self, 'websocket'):
         self.close_connection = True
         # prevents the Date header from being written
         self.provided_date = True
-
-
-
-#class MessageHandler(object):
-#    def __init__(self, environ, interfaces):
-#        self.ws = environ['wsgi.websocket']
-#        self.interfaces = interfaces
-#
-#        if self.ws.path in interfaces.keys():
-#            self.active_interface = interfaces[self.ws.path]
-#        else:
-#            raise Exception("no interface found")
-#
-#        self.on_open()
-#        self._handle()
-#
-#    def _handle(self):
-#        while True:
-#            message = self.ws.receive()
-#
-#            if message is None:
-#                self.active_interface.on_close()
-#                break
-#            else:
-#                self.active_interface.on_message(message)
-

geventwebsocket/protocols/message.py

+class MessageProtocol(object):
+    def __init__(self, environ, interfaces):
+        self.ws = environ['wsgi.websocket']
+        self.interfaces = interfaces
+
+        if self.ws.path in interfaces.keys():
+            self.active_interface = interfaces[self.ws.path]
+        else:
+            raise Exception("no interface found")
+
+        self.on_open()
+        self._handle()
+
+    def _handle(self):
+        while True:
+            message = self.ws.receive()
+
+            if message is None:
+                self.active_interface.on_close()
+                break
+            else:
+                self.active_interface.on_message(message)
+

geventwebsocket/server.py

 
     def __init__(self, *args, **kwargs):
         self.debug = kwargs.pop('debug', False)
+        self.pre_start_hook = kwargs.pop('pre_start_hook', None)
+
         self._logger = None
 
         kwargs['handler_class'] = WebSocketHandler
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.