How to use the socketshark.backend.websockets.serve function in socketshark

To help you get started, we’ve selected a few socketshark examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github closeio / socketshark / socketshark / backend / websockets.py View on Github external
async def serve(websocket, path):
            # If there are any pending connections that were established after
            # calling close() but before this callback was executed, close
            # them immediately.
            if self._closed:
                self.shark.log.warn('dropped connection',
                                    remote=websocket.remote_address)
                return
            client = Client(self.shark, websocket)
            await client.consumer_handler()

        config = self.shark.config
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.shark.prepare())
        ssl_context = self.shark.get_ssl_context()
        start_server = websockets.serve(serve,
                                        config['WS_HOST'],
                                        config['WS_PORT'],
                                        ssl=ssl_context)
        self.server = loop.run_until_complete(start_server)
        self.shark.signal_ready()
        loop.run_until_complete(self.shark.run())
        loop.run_forever()
        loop.run_until_complete(self.shutdown())
        self.shark.signal_shutdown()