def test_websocket_demultiplexer_send(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def receive(self, content, multiplexer=None, **kwargs): import pdb pdb.set_trace() # breakpoint 69f2473b // self.send(content) class Demultiplexer(websockets.WebsocketDemultiplexer): consumers = {"mystream": MyWebsocketConsumer} with apply_routes([ route_class(Demultiplexer, path='/path/(?P<id>\d+)'), route_class(MyWebsocketConsumer), ]): client = Client() with self.assertRaises(SendNotAvailableOnDemultiplexer): client.send_and_consume( 'websocket.receive', { 'path': '/path/1', 'text': json.dumps({ "stream": "mystream", "payload": { "text_field": "mytext" } }) }) client.receive()
def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes([route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', {'path': '/path/1', 'text': 'text'}) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_websocket_custom_json_serialization(self): class WebsocketConsumer(websockets.JsonWebsocketConsumer): @classmethod def decode_json(cls, text): obj = json.loads(text) return dict((key.upper(), obj[key]) for key in obj) @classmethod def encode_json(cls, content): lowered = dict((key.lower(), content[key]) for key in content) return json.dumps(lowered) def receive(self, content, multiplexer=None, **kwargs): self.content_received = content self.send({"RESPONSE": "HI"}) class MyMultiplexer(websockets.WebsocketMultiplexer): @classmethod def encode_json(cls, content): lowered = dict((key.lower(), content[key]) for key in content) return json.dumps(lowered) with apply_routes([route_class(WebsocketConsumer, path='/path')]): client = HttpClient() consumer = client.send_and_consume('websocket.receive', path='/path', text={"key": "value"}) self.assertEqual(consumer.content_received, {"KEY": "value"}) self.assertEqual(client.receive(), {"response": "HI"}) client.join_group('test_group') WebsocketConsumer.group_send('test_group', {"KEY": "VALUE"}) self.assertEqual(client.receive(), {"key": "VALUE"})
def test_websockets_demultiplexer_custom_multiplexer(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def connect(self, message, multiplexer=None, **kwargs): multiplexer.send({"THIS_SHOULD_BE_LOWERCASED": "1"}) class MyMultiplexer(websockets.WebsocketMultiplexer): @classmethod def encode_json(cls, content): lowered = { "stream": content["stream"], "payload": dict((key.lower(), content["payload"][key]) for key in content["payload"]) } return json.dumps(lowered) class Demultiplexer(websockets.WebsocketDemultiplexer): multiplexer_class = MyMultiplexer consumers = { "mystream": MyWebsocketConsumer } with apply_routes([route_class(Demultiplexer, path='/path/(?P<ID>\d+)')]): client = HttpClient() client.send_and_consume('websocket.connect', path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": {"this_should_be_lowercased": "1"}, })
def test_websocket_custom_json_serialization(self): class WebsocketConsumer(websockets.JsonWebsocketConsumer): @classmethod def decode_json(cls, text): obj = json.loads(text) return dict((key.upper(), obj[key]) for key in obj) @classmethod def encode_json(cls, content): lowered = dict((key.lower(), content[key]) for key in content) return json.dumps(lowered) def receive(self, content, multiplexer=None, **kwargs): self.content_received = content self.send({"RESPONSE": "HI"}) class MyMultiplexer(websockets.WebsocketMultiplexer): @classmethod def encode_json(cls, content): lowered = dict((key.lower(), content[key]) for key in content) return json.dumps(lowered) with apply_routes([route_class(WebsocketConsumer, path='/path')]): client = WSClient() consumer = client.send_and_consume('websocket.receive', path='/path', text={"key": "value"}) self.assertEqual(consumer.content_received, {"KEY": "value"}) self.assertEqual(client.receive(), {"response": "HI"}) client.join_group('test_group') WebsocketConsumer.group_send('test_group', {"KEY": "VALUE"}) self.assertEqual(client.receive(), {"key": "VALUE"})
def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual(client.send_and_consume('test.create').called, 'create') self.assertEqual(client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_websockets_demultiplexer_custom_multiplexer(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def connect(self, message, multiplexer=None, **kwargs): multiplexer.send({"THIS_SHOULD_BE_LOWERCASED": "1"}) class MyMultiplexer(websockets.WebsocketMultiplexer): @classmethod def encode_json(cls, content): lowered = { "stream": content["stream"], "payload": dict((key.lower(), content["payload"][key]) for key in content["payload"]) } return json.dumps(lowered) class Demultiplexer(websockets.WebsocketDemultiplexer): multiplexer_class = MyMultiplexer consumers = {"mystream": MyWebsocketConsumer} with apply_routes( [route_class(Demultiplexer, path='/path/(?P<ID>\d+)')]): client = WSClient() client.send_and_consume('websocket.connect', path='/path/1') self.assertEqual( client.receive(), { "stream": "mystream", "payload": { "this_should_be_lowercased": "1" }, })
def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual( client.send_and_consume('test.create').called, 'create') self.assertEqual( client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes( [route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', { 'path': '/path/1', 'text': 'text' }) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_websockets_demultiplexer(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def connect(self, message, multiplexer=None, **kwargs): multiplexer.send(kwargs) def disconnect(self, message, multiplexer=None, **kwargs): multiplexer.send(kwargs) def receive(self, content, multiplexer=None, **kwargs): multiplexer.send(content) class Demultiplexer(websockets.WebsocketDemultiplexer): consumers = {"mystream": MyWebsocketConsumer} with apply_routes( [route_class(Demultiplexer, path='/path/(?P<id>\d+)')]): client = WSClient() client.send_and_consume('websocket.connect', path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": { "id": "1" }, }) client.send_and_consume('websocket.receive', text={ "stream": "mystream", "payload": { "text_field": "mytext" }, }, path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": { "text_field": "mytext" }, }) client.send_and_consume('websocket.disconnect', path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": { "id": "1" }, })
def test_websockets_decorators(self): class WebsocketConsumer(websockets.WebsocketConsumer): strict_ordering = True def connect(self, message, **kwargs): self.order = message['order'] with apply_routes([route_class(WebsocketConsumer, path='/path')]): client = Client() client.send('websocket.connect', {'path': '/path', 'order': 1}) client.send('websocket.connect', {'path': '/path', 'order': 0}) client.consume('websocket.connect') self.assertEqual(client.consume('websocket.connect').order, 0) self.assertEqual(client.consume('websocket.connect').order, 1)
def test_websockets_decorators(self): class WebsocketConsumer(websockets.WebsocketConsumer): slight_ordering = True def connect(self, message, **kwargs): self.order = message['order'] with apply_routes([route_class(WebsocketConsumer, path='/path')]): client = Client() client.send('websocket.connect', {'path': '/path', 'order': 1}) client.send('websocket.connect', {'path': '/path', 'order': 0}) client.consume('websocket.connect') self.assertEqual(client.consume('websocket.connect').order, 0) self.assertEqual(client.consume('websocket.connect').order, 1)
def test_websockets_demultiplexer(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def connect(self, message, multiplexer=None, **kwargs): multiplexer.send(kwargs) def disconnect(self, message, multiplexer=None, **kwargs): multiplexer.send(kwargs) def receive(self, content, multiplexer=None, **kwargs): multiplexer.send(content) class Demultiplexer(websockets.WebsocketDemultiplexer): consumers = { "mystream": MyWebsocketConsumer } with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]): client = HttpClient() client.send_and_consume('websocket.connect', path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": {"id": "1"}, }) client.send_and_consume('websocket.receive', text={ "stream": "mystream", "payload": {"text_field": "mytext"}, }, path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": {"text_field": "mytext"}, }) client.send_and_consume('websocket.disconnect', path='/path/1') self.assertEqual(client.receive(), { "stream": "mystream", "payload": {"id": "1"}, })
def test_websockets_http_session_and_channel_session(self): class WebsocketConsumer(websockets.WebsocketConsumer): http_user_and_session = True user_model = get_user_model() user = user_model.objects.create_user(username='******', email='*****@*****.**', password='******') client = HttpClient() client.force_login(user) with apply_routes([route_class(WebsocketConsumer, path='/path')]): connect = client.send_and_consume('websocket.connect', {'path': '/path'}) receive = client.send_and_consume('websocket.receive', {'path': '/path'}, text={'key': 'value'}) disconnect = client.send_and_consume('websocket.disconnect', {'path': '/path'}) self.assertEqual( connect.message.http_session.session_key, receive.message.http_session.session_key ) self.assertEqual( connect.message.http_session.session_key, disconnect.message.http_session.session_key )
def test_websocket_demultiplexer_send(self): class MyWebsocketConsumer(websockets.JsonWebsocketConsumer): def receive(self, content, multiplexer=None, **kwargs): self.send(content) class Demultiplexer(websockets.WebsocketDemultiplexer): consumers = { "mystream": MyWebsocketConsumer } with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]): client = HttpClient() with self.assertRaises(SendNotAvailableOnDemultiplexer): client.send_and_consume('websocket.receive', path='/path/1', text={ "stream": "mystream", "payload": {"text_field": "mytext"}, }) client.receive()
from channels import route_class, route from values.consumers import Demultiplexer from values.models import IntegerValueBinding # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(Demultiplexer), route("binding.intval", IntegerValueBinding.consumer), ]
from channels import route_class from .consumers import PresenceConsumer # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. WebSocket messages of all # types have a 'path' attribute, so we're using that to route the socket. # While this is under stream/ compared to the HTML page, we could have it on the # same URL if we wanted; Daphne separates by protocol as it negotiates with a browser. channel_routing = [ route_class(PresenceConsumer, path=r'^/ws/$'), ]
from channels import route, route_class from channels.staticfiles import StaticFilesConsumer from .logserver import LogSubscriptionConsumer channel_routing = [ route('http.request', StaticFilesConsumer()), route_class(LogSubscriptionConsumer, path=r"^/log(?P<path>.+)"), ]
from channels import route_class from codenerix_pos.consumers import POSConsumer # from channels.routing import route # from codenerix_pos.consumers import ws_message, ws_connect, ws_disconnect channel_routing = [ route_class(POSConsumer, path=r"^/codenerix_pos/"), ] # channel_routing = [ # route("websocket.connect", ws_connect, path=r"^\/codenerix_pos\/(?P<uid>[a-zA-Z0-9_]+)$"), # route("websocket.receive", ws_message, path=r"^/codenerix_pos/(?P<uid>[a-zA-Z0-9_]+)$"), # route("websocket.disconnect", ws_disconnect, path=r"^/codenerix_pos/(?P<uid>[a-zA-Z0-9_]+)$"), # ]
from channels import route, route_class from inbox.consumers import ChatServer channel_routing = [ route_class(ChatServer, path=r"^/realtime/"), ]
from channels import route_class, route from values.consumers import Demultiplexer from values.models import IntegerValueBinding # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(Demultiplexer, path='^/stream/?$'), route("binding.intval", IntegerValueBinding.consumer), ]
from channels import route, route_class from branches.consumers import ServerDetailPageConsumer channel_routing = [ # route_class(ServerDetailPageConsumer, path="^/server/(?P<server>[\w\-_]+)"), route_class(ServerDetailPageConsumer, path="^/(?P<owner>[\w\-_])+/(?P<project>[\w\-_]+)/(?P<server>[\w\-_]+)"), # route("websocket.connect", "branches.consumers.server_page", path="^/server/(?P<server>[\w\-_]+)"), route("websocket.disconnect", "branches.consumers.server_page_disconnect", path="^/server/(?P<server>[\w\-_]+)"), route("websocket.receive", "branches.consumers.ws_receive"), ]
#-*- coding:utf-8 -*- from channels import route_class, route from devops.consumers import webterminal,SshTerminalMonitor # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(webterminal,path = r'^/ws'), route_class(SshTerminalMonitor,path= r'^/monitor/(?P<channel>\w+-\w+-\w+-\w+-\w+-\w+)'), ]
from blocks.consumers.info import display_info from blocks.consumers.websockets import ws_connect, ws_disconnect, ws_receive from blocks.consumers.angular_consumers.demultiplexers import BlockDemultiplexer, LatestBlocksDemultiplexer channel_routing = [ # Blocks route('parse_block', parse_block), route('repair_block', repair_block), route('check_block_hash', check_block_hash), # Transactions route('repair_transaction', repair_transaction), # Addresses route('parse_address', parse_address), # Info route('display_info', display_info), # Demultiplexing route_class(BlockDemultiplexer, path="^/block/$"), route_class(LatestBlocksDemultiplexer, path="^/latest_blocks/$"), # Websockets route('websocket.connect', ws_connect), route('websocket.disconnect', ws_disconnect), route('websocket.receive', ws_receive), ]
from channels import route_class, route from guacamole.consumers import GuacamoleWebsocket # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(GuacamoleWebsocket, path=r'^/ws'), ]
from channels import route_class from channels.generic.websockets import WebsocketDemultiplexer from api.bindings import GameBinding class APIDemultiplexer(WebsocketDemultiplexer): http_user = True consumers = {'games': GameBinding.consumer} channel_routing = [ route_class(APIDemultiplexer), ]
from channels import route_class from . import consumers channel_routing = [ route_class(consumers.SensorConsumer, path=r'^/room/[0-9A-F]{16}/sensor'), route_class(consumers.ClientConsumer, path=r'^/room/[0-9A-F]{16}/client'), route_class(consumers.ClientConsumer, path=r'^/room/[0-9A-F]{16}/stats'), route_class(consumers.ClientConsumer, path=r'^/room/[0-9A-F]{16}/map'), ]
from minesweeper import consumers from channels import route_class channel_routing = [ route_class(consumers.GameConsumer, path=r"^/minesweeper/stream/(?P<game_id>[^/]+)") ]
from channels import route, route_class from channels.generic.websockets import WebsocketDemultiplexer from posts.bindings import LiveblogResource class APIDemultiplexer(WebsocketDemultiplexer): mapping = { 'liveblogs': 'liveblogs' } # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. WebSocket messages of all # types have a 'path' attribute, so we're using that to route the socket. # While this is under stream/ compared to the HTML page, we could have it on the # same URL if we wanted; Daphne separates by protocol as it negotiates with a browser. channel_routing = [ route_class(APIDemultiplexer), route("liveblogs", LiveblogResource.consumer) # A default "http.request" route is always inserted by Django at the end of the routing list # that routes all unmatched HTTP requests to the Django view system. If you want lower-level # HTTP handling - e.g. long-polling - you can do it here and route by path, and let the rest # fall through to normal views. ]
from channels import route, route_class from main import consumers channel_routing = [ route_class(consumers.WSConsumer, path=r"^/$"), ]
from channels import route_class, route from webterminallte.consumers import Webterminal, SshTerminalMonitor # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class( Webterminal, path= r'^/ws/(?P<ip>(?:(?:0|1[\d]{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.){3}(?:0|1[\d]{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?))/(?P<id>[\w]+)/$' ), route_class(SshTerminalMonitor, path=r'^/monitor/(?P<channel>[\w-]+)/(?P<id>[\w]+)/$'), ]
from channels import route_class from channelstest import consumers channel_routing = [route_class(consumers.ChannelsTestConsumer)]
from channels import route, route_class from myproject.core.websocket_handler import NewsConsumer channel_routing = [ route_class(NewsConsumer, path=r"^/chat/"), ]
from channels import route_class, route from webterminal.consumers import webterminal, CommandExecute, SshTerminalMonitor, BatchCommandExecute from guacamole.consumers import GuacamoleWebsocket, GuacamoleMonitor # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(webterminal, path=r'^/ws'), route_class(CommandExecute, path=r'^/execute'), route_class(SshTerminalMonitor, path=r'^/monitor/(?P<channel>\w+-\w+-\w+-\w+-\w+-\w+)'), route_class(GuacamoleWebsocket, path=r'^/guacamole/(?P<id>[0-9]+)/'), route_class(GuacamoleMonitor, path=r'^/guacamolemonitor/(?P<id>[0-9]+)/'), route_class(BatchCommandExecute, path=r'^/batchexecute/'), ]
# -*- coding: utf-8 -*- from channels import route_class from webterminal.consumers import Webterminal, SshTerminalMonitor, BatchCommandExecute from ansible_task.consumers import AnsModuleConsumer, AnsPlaybookConsumer channel_routing = [ route_class(Webterminal, path=r'^/ws/webssh/'), route_class(AnsModuleConsumer, path=r'^/ws/adhocrun/'), route_class(AnsPlaybookConsumer, path=r'^/ws/playbookrun/'), route_class(SshTerminalMonitor, path=r'^/monitor/(?P<channel>[\w-]+)'), route_class(BatchCommandExecute, path=r'^/batchexecute/'), ]
from channels import route_class, route from webterminal.consumers import webterminal, CommandExecute, SshTerminalMonitor # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(webterminal, path=r'^/ws'), route_class(CommandExecute, path=r'^/execute'), route_class(SshTerminalMonitor, path=r'^/monitor/(?P<channel>\w+-\w+-\w+-\w+-\w+-\w+)'), ]
from channels import route, route_class from chat import consumers as chatConsumer from notification import consumers as notifyConsumer channel_routing = [ #route_class(chatConsumer.MyConsumer), route_class(chatConsumer.MyConsumer, path=r'/doc/[0-9]+'), route_class(notifyConsumer.MyConsumer, path=r'/notify'), ]
from channels import (route, route_class) from .bindings import Demultiplexer from .consumers import Consumer from .views import chat_receive websocket_routing = [ route_class(Demultiplexer, path=r"^/bindings$"), Consumer.as_route(path=r"^/(?P<channel>[a-zA-Z0-9_]+)$"), ] custom_routing = [ route("chat.receive", chat_receive), ]
from channels import route, route_class from .consumers import ChatServer, events, Demultiplexer, RoomBinding chat_routing = [ route_class(ChatServer, path=r'^(?P<slug>[^/]+)/stream/$'), ] event_routing = [ route('chat.receive', events.user_join, event=r'^user-join$'), route('chat.receive', events.user_leave, event=r'^user-leave$'), route('chat.receive', events.client_send, event=r'^message-send$'), ] binding_routing = [ route_class(Demultiplexer, path=r'^/binding/'), route('binding.room', RoomBinding.consumer), ]
from channels import route_class, route from OpsManage.consumers import webterminal # The channel routing defines what channels get handled by what consumers, # including optional matching on message attributes. In this example, we route # all WebSocket connections to the class-based BindingConsumer (the consumer # class itself specifies what channels it wants to consume) channel_routing = [ route_class(webterminal, path=r'^/ws'), ]
from channels import route_class from socialhome.streams.consumers import StreamConsumer channel_routing = [ route_class(StreamConsumer, path=r'^/ch/streams/(?P<stream>[^/]+)/$'), ]
from channels import route_class from excon_service.consumers import Demultiplexer trip_service_routing = [ ] channel_routing = [ route_class(Demultiplexer, path="^/api/v1/ws"), ]
from channels import route_class from channels.generic.websockets import WebsocketDemultiplexer from .test_bindings import TestModelResourceBinding class TestDemultiplexer(WebsocketDemultiplexer): http_user_and_session = True consumers = { 'testmodel': TestModelResourceBinding.consumer } channel_routing = [ route_class(TestDemultiplexer) ]