django channels redis关闭时间太长,被杀死了



所以我试着用通道做一些基本的东西。我在HTML中编写了一个脚本来返回消息

脚本

<script>
const chatsocket = new WebSocket(
'ws://'+window.location.host+'/ws/test/'
)
chatsocket.onmessage = function (e) {
const data = JSON.parse(e.data)
console.log(data)
chatsocket.close()
}
</script>

我consumers.py

from channels.exceptions import StopConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
import json

class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'notificationgroup'
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
await self.accept()
await self.channel_layer.group_send(
self.group_name,
{
'type': 'tester_message',
'tester': 'hello world'
}
)
async def tester_message(self, event):
tester = event['tester']
await self.send(text_data=json.dumps({
'tester': tester
}))
async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
raise StopConsumer()
所以基本上我在控制台中得到了我想要的输出…当我有
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer'
}
}

但是当我使用aws redis集群(在安装channels-redis之后)

CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('aws redis endpoint', 6379)],
},
},
}

我得到这个错误

WebSocket HANDSHAKING /ws/test/ [127.0.0.1:51551]
WebSocket DISCONNECT /ws/test/ [127.0.0.1:51551]
Application instance <Task pending name='Task-4' coro=<StaticFilesWrapper.__call__() running at E:intessyncsherovenvlibsite-packageschannelsstaticfiles.py:44> wait_for=<Future pending cb=[BaseSelectorEventLoop._sock_write_done(1216)(), <TaskWakeupMethWrapper object at 0x000001F7D5226C70>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 51551] path=b'/ws/test/'> took too long to shut down and was killed.

我做错了什么?

我做错了。显然,你无法让法律系统在外面工作。您的应用程序必须在同一个VPN内的EC2上运行。然后,它成功了!

最新更新