Python 卡在AsyncWebsocketConsumer实现上
我正在尝试开发一个消费者(AsyncWebsocketConsumer类型),它将与websocket连接,并使用JavaScript对前端进行更改。 我未能实现的第一件事是消费者的功能(连接、发送、断开连接)。另外,使用Redis 我的设置.py是Python 卡在AsyncWebsocketConsumer实现上,python,redis,django-channels,channels,Python,Redis,Django Channels,Channels,我正在尝试开发一个消费者(AsyncWebsocketConsumer类型),它将与websocket连接,并使用JavaScript对前端进行更改。 我未能实现的第一件事是消费者的功能(连接、发送、断开连接)。另外,使用Redis 我的设置.py是 ASGI_APPLICATION=“myapp.routing.APPLICATION” 通道_层={ “默认值”:{ “后端”:“channels\u redis.core.RedisChannelLayer”, “配置”:{ '主机':[('l
ASGI_APPLICATION=“myapp.routing.APPLICATION”
通道_层={
“默认值”:{
“后端”:“channels\u redis.core.RedisChannelLayer”,
“配置”:{
'主机':[('localhost',6379)],
},
}
}
和myrouting.py是
application=ProtocolTypeRouter({
“信道”:信道名称路由器({
“示例”:示例消费者,
}),
})
最后,我的consumers.py是类示例消费者(AsyncWebsocketConsumer):
异步def connect(self,msg):
#接通电话。
#要接受连接呼叫,请执行以下操作:
等待自我接受
打印('通道已连接')
当我尝试时:channel\u layer=get\u channel\u layer()
异步到同步(channel\u layer.send)('example',{'type':'connect'})
因此,我可以调用connect并查看已连接消息,该消息将让我知道套接字已连接,然后通过发送消息继续,我得到:
raise NOTEImplementedError(“您必须实现应用程序_send()”)
您必须实现应用程序_send()
我很确定我误解了这么多东西,但我一直在寻找如何解决这个问题,我找不到任何对我的案例有用的东西,比如一个例子或好的文档,所以无论有什么帮助都将不胜感激 您使用的ChannelLayers错误。它们用于在应用程序的不同实例之间进行通信。不用于实际建立WebSocket连接 试试这个: 设置.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
routing.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
consumers.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
您可以使用测试来测试ws端点
希望这能帮助您开始。您使用的ChannelLayers是错误的。它们用于在应用程序的不同实例之间进行通信。不用于实际建立WebSocket连接 试试这个: 设置.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
routing.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
consumers.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
async def receive(self, data):
# do something with data
print(data)
# send response back to connected client
await self.send('We received your message')
您可以使用测试来测试ws端点