コード例 #1
0
 def _make_backend(self, name: str, config):
     # Check for old format config
     if "ROUTING" in self.configs[name]:
         raise InvalidChannelLayerError(
             "ROUTING key found for %s - this is no longer needed in Channels 2."
             % name)
     # Load the backend class
     try:
         backend_class = import_string(self.configs[name]["BACKEND"])
     except KeyError:
         raise InvalidChannelLayerError("No BACKEND specified for %s" %
                                        name)
     except ImportError:
         raise InvalidChannelLayerError(
             "Cannot import BACKEND %r specified for %s" %
             (self.configs[name]["BACKEND"], name))
     # Initialise and pass config
     return backend_class(**config)
コード例 #2
0
 def make_test_backend(self, name: str):
     """
     Instantiate channel layer using its test config.
     """
     try:
         config = self.configs[name]["TEST_CONFIG"]
     except KeyError:
         raise InvalidChannelLayerError("No TEST_CONFIG specified for %s" %
                                        name)
     return self._make_backend(name, config)
コード例 #3
0
 async def discord_ready(self, event):
     """
     Runs when initially connected to discord.  This method is primarily
     used for internal tasks, such as adding groups to the channel layer.
     Any tasks meant to be run on initially connecting should be called
     by overriding the `ready` method in your app's consumer
     """
     try:
         for group in self.groups:
             await self.channel_layer.group_add(group, self.channel_name)
     except AttributeError:
         raise InvalidChannelLayerError(
             "BACKEND is unconfigured or doesn't support groups")
     await self.ready()
コード例 #4
0
 async def websocket_disconnect(self, message: Scope) -> None:
     """
     Called when a WebSocket connection is closed. Base level so you don't
     need to call super() all the time.
     """
     try:
         for group in self.groups:
             await self.channel_layer.group_discard(group, self.channel_name)
     except AttributeError:
         raise InvalidChannelLayerError(
             "BACKEND is unconfigured or doesn't support groups"
         )
     await self.disconnect(message["code"])
     raise StopConsumer()
コード例 #5
0
    async def connect(self):
        await super().connect()
        await self.database_lookups()

        try:
            await self.channel_layer.group_add(self.room.identifier,
                                               self.channel_name)
        except AttributeError:
            raise InvalidChannelLayerError(
                "BACKEND is unconfigured or doesn't support groups")
        self.groups.append(self.room.identifier)

        # Send events to initialize board
        for event in await self.init_events():
            await self.send_json(await event.response_all_users())
コード例 #6
0
 def websocket_connect(self, message: Scope) -> None:
     """
     Called when a WebSocket connection is opened.
     """
     try:
         for group in self.groups:
             async_to_sync(self.channel_layer.group_add)(group, self.channel_name)
     except AttributeError:
         raise InvalidChannelLayerError(
             "BACKEND is unconfigured or doesn't support groups"
         )
     try:
         self.connect()
     except AcceptConnection:
         self.accept()
     except DenyConnection:
         self.close()
コード例 #7
0
    async def websocket_connect(self, message):
        # Check for valid session data
        await self.load_session()
        if "checksum" not in self.scope["session"]:
            await self.close(1008)
            return
        if "user_name" not in self.scope["session"]:
            await self.close(1008)
            return

        # Store relevant data
        user_name = self.scope["session"]["user_name"]
        meeting_id = self.scope["url_route"]["kwargs"]["meeting_id"]

        # Check checksum
        tmp_checksum = hashlib.sha512(
            f"{user_name}{meeting_id}{settings.SHARED_SECRET}".encode(
                "utf-8")).hexdigest()
        if self.scope["session"]["checksum"] != tmp_checksum:
            await self.close(1008)
            return

        # Setup channel group
        self.groups.append(meeting_id)
        try:
            for group in self.groups:
                await self.channel_layer.group_add(group, self.channel_name)
        except AttributeError:
            raise InvalidChannelLayerError(
                "BACKEND is unconfigured or doesn't support groups")

        # Accept connection
        await self.accept()

        # Increment the viewer count by 1
        await viewers[meeting_id].increment()

        # Save meeting and user
        # => When these aren't set, this method didn't terminate correctly and the counter wasn't incremented
        self.meeting_id = meeting_id