def _make_backend(self, name, config): # 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)
async def group_send(self, service: str, message: dict) -> None: """Send message to service channel group.""" try: await self.channel_layer.group_send(self.channel_group_name(service), message) except AttributeError: raise InvalidChannelLayerError( "BACKEND is not configured or doesn't support groups")
async def send_to_group(self, group: str, message: dict) -> None: """Sends the given message to the given group.""" try: await self.channel_layer.group_send(group, message) except AttributeError: raise InvalidChannelLayerError( "BACKEND is not configured or doesn't support groups")
async def on_connection_close(self): # Remove channel groups try: for group in self.groups: await self.group_discard(group) except AttributeError: raise InvalidChannelLayerError( "BACKEND is not configured or doesn't support groups") super().on_connection_close()
def make_test_backend(self, name): """ 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)
async def open(self, *args, **kwargs): """Invoked when a new WebSocket is opened.""" await super().open(*args, **kwargs) IOLoop.current().add_callback(self.channel_receive_callback) # Initialize channel layer self.channel_layer = get_channel_layer() if self.channel_layer is not None: self.channel_name = await self.channel_layer.new_channel() self.channel_receive = functools.partial( self.channel_layer.receive, self.channel_name) # Add channel groups groups = await self.get_groups() or [] try: for group in groups: await self.group_add(group) except AttributeError: raise InvalidChannelLayerError( "BACKEND is not configured or doesn't support groups")