从这个非常简单的工作代码示例开始:
from channels.generic.websocket import JsonWebsocketConsumer
class IsacomptaManagementFeesConsumer(JsonWebsocketConsumer):
pass
从 javascript 连接到此 websocket 使用者时,这按预期工作。连接正确发出,我得到以下日志:
WebSocket HANDSHAKING /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38108]
WebSocket CONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38108]
WebSocket DISCONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38108]
现在,如果我将代码更改为以下代码,以使用组:
from channels.generic.websocket import JsonWebsocketConsumer
class IsacomptaManagementFeesConsumer(JsonWebsocketConsumer):
groups = ['foobar']
然后,连接失败。JavaScript 控制台告诉我:
Firefox can’t establish a connection to the server at ws://antoine.cocoonr.hq:3001/manager/accounting/isacompta/2020/03/management-fees.ws.
error { target: WebSocket, isTrusted: true, srcElement: WebSocket, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, composed: false, timeStamp: 7804, … }
服务器日志如下所示:
WebSocket HANDSHAKING /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38128]
WebSocket DISCONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38128]
但在服务器端没有引发异常。
我也可以在不使用组的情况下获得类似的行为。让我们看一个更大的工作代码示例:
from channels.generic.websocket import JsonWebsocketConsumer
class IsacomptaManagementFeesConsumer(JsonWebsocketConsumer):
def connect(self):
print("one")
self.accept()
print("two")
self.send_json({'text': "Foobar"})
print("three")
此代码工作正常,连接正确发出,这里是服务器日志:
WebSocket HANDSHAKING /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38168]
one
WebSocket CONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38168]
two
three
WebSocket DISCONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38168]
但是,如果我对我的代码进行以下小改动:
from asgiref.sync import async_to_sync
from channels.generic.websocket import JsonWebsocketConsumer
class IsacomptaManagementFeesConsumer(JsonWebsocketConsumer):
def connect(self):
print("one")
self.accept()
print("two")
async_to_sync(self.channel_layer.send)(self.channel_name, {
'type': 'foobar.send',
'text': "Foobar",
})
print("three")
def foobar_send(self, event):
print("AAA")
self.send_json({'text': event['text'])
print("BBB")
然后,连接正确发出但立即关闭,进程停止,没有机会打印“三”,函数“foobar_send”也没有执行。
WebSocket HANDSHAKING /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38224]
one
WebSocket CONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38224]
two
WebSocket DISCONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:38224]
我不明白为什么没有打印“三”,同时也没有引发异常。这意味着异常被?self.channel_layers.send()
的调用者引发并静默捕获。IsacomptaManagementFeesConsumer.connect()
写完最后一段后,我决定试一试:
import traceback
from asgiref.sync import async_to_sync
from channels.generic.websocket import JsonWebsocketConsumer
class IsacomptaManagementFeesConsumer(JsonWebsocketConsumer):
def connect(self):
print("one")
self.accept()
print("two")
try:
async_to_sync(self.channel_layer.send)(self.channel_name, {
'type': 'foobar.send',
'text': "Foobar",
})
except Exception as e:
print(e)
traceback.print_stack()
print("three")
def foobar_send(self, event):
print("AAA")
self.send_json({'text': event['text'])
print("BBB")
就是这样,有一个隐藏的“找不到文件”错误:
WebSocket HANDSHAKING /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:42562]
one
WebSocket CONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:42562]
two
[Errno 2] No such file or directory
File "/usr/lib64/python3.6/threading.py", line 884, in _bootstrap
self._bootstrap_inner()
File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib64/python3.6/concurrent/futures/thread.py", line 69, in _worker
work_item.run()
File "/usr/lib64/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/tony/.venvs/cocoonr/lib/python3.6/site-packages/channels/db.py", line 14, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
File "/home/tony/.venvs/cocoonr/lib/python3.6/site-packages/asgiref/sync.py", line 277, in thread_handler
return func(*args, **kwargs)
File "/home/tony/.venvs/cocoonr/lib/python3.6/site-packages/channels/consumer.py", line 105, in dispatch
handler(message)
File "/home/tony/.venvs/cocoonr/lib/python3.6/site-packages/channels/generic/websocket.py", line 39, in websocket_connect
self.connect()
File "/home/tony/Workspace/cocoonr/billing/consumers.py", line 32, in connect
traceback.print_stack()
three
WebSocket DISCONNECT /manager/accounting/isacompta/2020/03/management-fees.ws [192.168.96.1:42562]
我将 Django 3.0 与 Channels 2.4.0 和 channels-redis 2.4.2 一起使用。这是我的 CHANNEL_LAYERS 设置:
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [
{
'address': get_env('CHANNELS_REDIS_HOST',
default='localhost:6379'),
'password': get_env('CHANNELS_REDIS_PASSWORD',
default=None),
'db': 1,
},
],
},
},
}