ジャンゴチャンネル
ジャンゴ・ジャンゴチャンネルサポート用WebSocketのモジュールです。
インストール1.
1
|
pip3 install channels
|
2.クイックスタート
2.1の構成設定を追加します
INSTALLED_APPS = [ 'django.contrib.admin'、 'django.contrib.auth'、 'django.contrib.contenttypes'、 'django.contrib.sessions'、 'django.contrib.messages'、 'django.contrib.staticfiles' 、 'チャンネル'、 ]
ASGI_APPLICATION = "django_channels_demo.routing.application"
2.2アプリケーション用WebSocketを作成し、ルーティング
#!は/ usr / binに/のenvのpython # - * -コーディング:UTF-8 - * - インポートProtocolTypeRouter、URLRouterをchannels.routingから URLをインポートdjango.conf.urlsから チャット輸入消費者から アプリケーション= ProtocolTypeRouter({ 'WebSocketの': URLRouter([ URL(R '^チャット/ $'、consumers.ChatConsumer)、 ]) })
2.3書き込み処理ビジネス・ロジック用WebSocket
#!は/ usr /ビン/ envをPythonの # - * -コーディング:UTF-8 - * - channels.generic.websocketインポートWebsocketConsumerから channels.exceptionsインポートStopConsumerから クラスChatConsumer(WebsocketConsumer): DEF websocket_connect(セルフ、メッセージ): セルフ.accept() DEF websocket_receive(セルフ、メッセージ): 印刷( 'メッセージを受信'、メッセージ) self.send(text_data = '受信') DEF websocket_disconnect(セルフ、メッセージ): 印刷(「クライアントが切断「) 昇給StopConsumer()
#!は/ usr / binに/のenvのpython # - * -コーディング:UTF-8 - * - channels.generic.websocketインポートWebsocketConsumerから channels.exceptionsからインポートStopConsumer クラスSimpleChatConsumer(WebsocketConsumer): デフ接続(自己): self.accept () :DEF(自己、text_data =なし、bytes_data =なし)受信 self.send(text_data) #主动断开连接 #self.close() :DEF切断(自己、コード) プリント( '客户端要断开了「)
#!は/ usr / binに/のenvのpython # - * -コーディング:UTF-8 - * - channels.generic.websocketインポートWebsocketConsumerから channels.exceptionsからの輸入StopConsumer CLIENTS = [] クラスChatConsumer(WebsocketConsumer): デフ接続(自己) : self.accept() CLIENTS.append(自己) DEF受信(自己、text_data =なし、bytes_data =なし): 項目のクライアントで: item.send(text_data) #主动断开连接 #self.close() DEF切断(自己、コード): CLIENTS.remove(自己)
3.チャネル層
メモリベースのチャネル層
CHANNEL_LAYERS = { "デフォルト":{ "BACKEND": "channels.layers.InMemoryChannelLayer" } }
channels.generic.websocketインポートWebsocketConsumerから asgiref.sync輸入async_to_syncのから :クラスChatConsumer(WebsocketConsumer) デフ接続(自己): async_to_sync(self.channel_layer.group_add)( 'X1'、self.channel_name) self.accept() defを受け取ります(自己、text_data =なし、bytes_data =なし): async_to_sync(self.channel_layer.group_send)( 'X1'、{ 'タイプ': 'xxx.ooo'、 'メッセージ':text_data }) デフxxx_ooo(自己、イベント) : メッセージ=イベント[ 'メッセージ'] self.send(メッセージ) DEF切断(自己、コード): async_to_sync(self.channel_layer.group_discard)( 'X1'、self.channel_name)
チャネル層のRedisに基づいて、
1
|
pip3 install channels
-
redis
|
CHANNEL_LAYERS = { "デフォルト":{ "BACKEND": "channels_redis.core.RedisChannelLayer"、 "CONFIG":{ "ホスト":[('10 .211.55.25' 、6379)] } } } CHANNEL_LAYERS = { 「既定'{ 'BACKEND': 'channels_redis.core.RedisChannelLayer'、 'CONFIG':{ "ホスト":[ "のRedis://10.211.55.25:1分の6379"]} } } CHANNEL_LAYERS = { '既定'{ 'BACKEND': 'channels_redis.core.RedisChannelLayer'、 'CONFIG':{ "ホスト":[('10 .211.55.25'、6379)]、}}、 } CHANNEL_LAYERS = { "デフォルト":{ "BACKEND": "channels_redis.core.RedisChannelLayer"、 "CONFIG":{ "ホスト":[ "のRedis://:[email protected]:6379/0"]、 "symmetric_encryption_keys":[SECRET_KEY] }、 }、 }
channels.generic.websocketインポートWebsocketConsumerから asgiref.sync輸入async_to_syncのから :クラスChatConsumer(WebsocketConsumer) デフ接続(自己): async_to_sync(self.channel_layer.group_add)( 'X1'、self.channel_name) self.accept() defを受け取ります(自己、text_data =なし、bytes_data =なし): async_to_sync(self.channel_layer.group_send)( 'X1'、{ 'タイプ': 'xxx.ooo'、 'メッセージ':text_data }) デフxxx_ooo(自己、イベント) : メッセージ=イベント[ 'メッセージ'] self.send(メッセージ) DEF切断(自己、コード): async_to_sync(self.channel_layer.group_discard)( 'X1'、self.channel_name)
ジャンゴ・ジャンゴチャンネルサポート用WebSocketのモジュールです。
インストール1.
1
|
pip3 install channels
|
2.クイックスタート
2.1の構成設定を追加します
INSTALLED_APPS = [ 'django.contrib.admin'、 'django.contrib.auth'、 'django.contrib.contenttypes'、 'django.contrib.sessions'、 'django.contrib.messages'、 'django.contrib.staticfiles' 、 'チャンネル'、 ]
ASGI_APPLICATION = "django_channels_demo.routing.application"
2.2アプリケーション用WebSocketを作成し、ルーティング
#!は/ usr / binに/のenvのpython # - * -コーディング:UTF-8 - * - インポートProtocolTypeRouter、URLRouterをchannels.routingから URLをインポートdjango.conf.urlsから チャット輸入消費者から アプリケーション= ProtocolTypeRouter({ 'WebSocketの': URLRouter([ URL(R '^チャット/ $'、consumers.ChatConsumer)、 ]) })
2.3書き込み処理ビジネス・ロジック用WebSocket
#!は/ usr /ビン/ envをPythonの # - * -コーディング:UTF-8 - * - channels.generic.websocketインポートWebsocketConsumerから channels.exceptionsインポートStopConsumerから クラスChatConsumer(WebsocketConsumer): DEF websocket_connect(セルフ、メッセージ): セルフ.accept() DEF websocket_receive(セルフ、メッセージ): 印刷( 'メッセージを受信'、メッセージ) self.send(text_data = '受信') DEF websocket_disconnect(セルフ、メッセージ): 印刷(「クライアントが切断「) 昇給StopConsumer()
#!/usr/bin/env python # -*- coding:utf-8 -*- from channels.generic.websocket import WebsocketConsumer from channels.exceptions import StopConsumer class SimpleChatConsumer(WebsocketConsumer): def connect(self): self.accept() def receive(self, text_data=None, bytes_data=None): self.send(text_data) # 主动断开连接 # self.close() def disconnect(self, code): print('客户端要断开了')
#!/usr/bin/env python # -*- coding:utf-8 -*- from channels.generic.websocket import WebsocketConsumer from channels.exceptions import StopConsumer CLIENTS = [] class ChatConsumer(WebsocketConsumer): def connect(self): self.accept() CLIENTS.append(self) def receive(self, text_data=None, bytes_data=None): for item in CLIENTS: item.send(text_data) # 主动断开连接 # self.close() def disconnect(self, code): CLIENTS.remove(self)
3. channel layer
基于内存的channel layer
CHANNEL_LAYERS = { "default": { "BACKEND": "channels.layers.InMemoryChannelLayer", } }
from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync class ChatConsumer(WebsocketConsumer): def connect(self): async_to_sync(self.channel_layer.group_add)('x1', self.channel_name) self.accept() def receive(self, text_data=None, bytes_data=None): async_to_sync(self.channel_layer.group_send)('x1', { 'type': 'xxx.ooo', 'message': text_data }) def xxx_ooo(self, event): message = event['message'] self.send(message) def disconnect(self, code): async_to_sync(self.channel_layer.group_discard)('x1', self.channel_name)
基于 redis的channel layer
1
|
pip3 install channels
-
redis
|
CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { "hosts": [('10.211.55.25', 6379)] }, }, } CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': {"hosts": ["redis://10.211.55.25:6379/1"],}, }, } CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': {"hosts": [('10.211.55.25', 6379)],},}, } CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { "hosts": ["redis://:[email protected]:6379/0"], "symmetric_encryption_keys": [SECRET_KEY], }, }, }
from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync class ChatConsumer(WebsocketConsumer): def connect(self): async_to_sync(self.channel_layer.group_add)('x1', self.channel_name) self.accept() def receive(self, text_data=None, bytes_data=None): async_to_sync(self.channel_layer.group_send)('x1', { 'type': 'xxx.ooo', 'message': text_data }) def xxx_ooo(self, event): message = event['message'] self.send(message) def disconnect(self, code): async_to_sync(self.channel_layer.group_discard)( 'X1'、self.channel_name)