【发布时间】:2019-06-11 07:03:22
【问题描述】:
通过 youtube 上的这些教程之一 我刚刚使用 django-channels 编写了一个 websocket 服务器 这是我的代码 信号.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
@receiver(post_save, sender=User)
def announce_new_user(sender, instance, created, **kwargs):
if created:
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
"gossip", {"type": "user_gossip",
"event": "New User",
"username": instance.username})
consumers.py
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class NoseyConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("gossip", self.channel_name)
print(f"Added {self.channel_name} channel to gossip")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("gossip", self.channel_name)
print(f"Removed {self.channel_name} channel to gossip")
async def user_gossip(self, event):
await self.send_json(event)
print(f"Got message {event} at {self.channel_name}")
apps.py
from django.apps import AppConfig
class NotifierConfig(AppConfig):
name = 'notifier'
def ready(self):
from .import signals
初始化.py
default_app_config = 'notifier.apps.NotifierConfig'
路由.py
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from notifier.consumers import NoseyConsumer
application = ProtocolTypeRouter({
"websocket": URLRouter([
path("notifications/", NoseyConsumer),
])
})
在 settings.py 中的 channels_layers 中
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost",16379)],
},
},
}
我不知道问题是什么,当我使用管理员创建新用户时,我看不到从 django websockets 通过控制台传输的任何类型的数据,并尝试了很多解决问题,但仍然没有用
这是我的客户端 javascript 代码
<html>
<head>
<script type = "text/javascript">
// Let us open a web socket
var ws = new WebSocket("ws://localhost:8000/notifications/");
var msg = "hi, this is simple message.";
ws.onopen = function(evt) {
ws.send(msg);
};
ws.onmessage = function(evt) {
// handle this message
console.log(evt.username);
};
ws.onclose = function(evt) {
// this channel is closed
};
ws.onerror = function(evt) {
// handle this error
};
</script>
</head>
<body>
</body>
</html>
当我尝试通过在终端中添加用户来访问同一网站时
HTTP GET / 200 [0.01, 127.0.0.1:55829]
WebSocket HANDSHAKING /notifications/ [127.0.0.1:55935]
WebSocket CONNECT /notifications/ [127.0.0.1:55935]
Added specific.pgRlAson!hqgpORLfvfbO channel to gossip
Exception inside application: Expecting value: line 1 column 1 (char 0)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\utils.py", line 52, in await_many_dispatch
await dispatch(result)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\consumer.py", line 73, in dispatch
await handler(message)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\generic\websocket.py", line 196, in websocket_
receive
await self.receive(text_data=message["text"])
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\generic\websocket.py", line 259, in receive
await self.receive_json(await self.decode_json(text_data), **kwargs)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\channels\generic\websocket.py", line 277, in decode_jso
n
return json.loads(text_data)
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
Expecting value: line 1 column 1 (char 0)
WebSocket DISCONNECT /notifications/ [127.0.0.1:55935]
【问题讨论】:
-
请编辑帖子以反映您根据我的回答对代码所做的更改。同样,您确定在发送信号时连接是活动的并且实际上正在发送信号吗?是否也可以在信号处理程序中添加打印语句,以确保连接打印语句后直接跟信号打印语句?
-
问题已更新 :-) 当我运行代码或添加用户时,我在终端中看不到这些打印语句,请您详细说明我应该添加哪些打印语句,很抱歉我对这些渠道如何在引擎盖下工作的知识为零
-
如果您没有看到打印语句,那么您尝试向其发送消息的客户端未连接,因此当然无法发送消息。我的意思是你应该在
announce_new_user接收器方法中添加一个打印语句。你的客户端是通过 websocket 连接到服务器的吗?? -
我在 signals.py 的第 4 行添加了一个打印语句,这是该教程的项目github.com/arocks/channels-example/tree/UserNotif 请看一下,所以作为一个新手,一切对我来说都太混乱了
-
如果
connect中的打印语句没有显示,那么答案很简单——你的js客户端没有连接到服务器。所以检查模板中的js代码和浏览器开发者工具,确保js客户端能够连接
标签: python django websocket django-channels