- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试开发一个消费者(AsyncWebsocketConsumer 类型),它将与 websocket 连接并使用 JavaScript 对前端进行更改。我未能实现的第一件事是消费者的功能(连接、发送、断开连接)。此外,使用 Redis。
我的 settings.py 是
ASGI_APPLICATION = "myapp.routing.application"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('localhost', 6379)],
},
}
}
而我的 routing.py 是
application = ProtocolTypeRouter({
"channel": ChannelNameRouter({
"example": ExampleConsumer,
}),
})
最后,我的 consumers.py 是
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self,msg):
# Called on connection.
# To accept the connection call:
await self.accept()
print('Channel connected')
当我尝试:
channel_layer = get_channel_layer()
async_to_sync(channel_layer.send)('example', {'type': 'connect'})
这样我就可以调用 connect 并查看让我知道套接字已连接的连接消息,然后继续发送消息,我得到:
raise NotImplementedError("You must implement application_send()") You must implement application_send()
我很确定我误解了很多东西,但我一直在寻找如何解决这个问题,但我找不到对我的案例有用的东西,比如示例或好的文档,所以无论有什么帮助都会不胜感激!
最佳答案
您使用的 ChannelLayers 有误。它们用于在应用程序的不同实例之间进行通信。不适用于实际建立 WebSocket 连接。
试试这个:
settings.py
ASGI_APPLICATION = "myapp.routing.application" # make sure your project is called 'myapp' ;-)
路由.py
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('ws/example/', consumers.ExampleConsumer),
])
),
})
消费者.py
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')
您可以使用 Simple Websocket Client测试你的 ws-endpoint。
希望这有助于您入门。
关于python - 停留在 AsyncWebsocketConsumer 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57557355/
这是我的代码 />100 1000 它不按要求工作.. 当提交表单时(并且在任何错误情况下)它返回到默认选中的单选按钮,即值 = 1000 用户必须再次单击值 = 100,而目标是,如果用户选择了 1
假设我有一个透明的红色 HTML 元素。当我悬停该元素时,它应该变成纯红色。当我停止悬停该元素时,它应该动画回到第一个状态,但仅在 X 秒后。 到目前为止一切顺利,请参阅代码片段。 我的问题是当我停止
我遇到了 cookie 情况,我的 cookie 会存储一个颜色名称或根本不存储任何内容。所以让我们这样解释吧。我的 cookie 与我网站的外观有关,我的网站有 3 种外观: 正常(完全没有 coo
这是我的问题。我有一张包含三个 div 的 Bootstrap v4 卡。 A 是最重要的一个,我希望它保持在左上角。 当页面较宽时,我希望 B 和 C 在 A 的右侧。 当页面变窄时,卡片缩小,C
示例表: uid time_stp traf 1 2016-01-13 00:00:00 6 1 2016-01-13 05:00:00 8 1
我是一名优秀的程序员,十分优秀!