I'm following the sample code provided by the channels documentation and have run into a issue. The django server successfully accepts a websocket from the browser and sending appears to work. However server-side processing of the message (ws_message) does not appear to occur, and no reply (nor any alert) is registered browser-side.
Sending seems to work, but no reply
This behavior is highly similar to that observed in Django channels - Echo example not working . However while switching to twisted 16.2.0 was the solution to that case, I am already on twisted 16.2.0.
Code snippets are as follows:
consumers.py
from django.http import HttpResponse
from channels.handler import AsgiHandler
def ws_message(message):
print("sending message ", message.content["text"])
raise
message.reply_channel.send({
"text": message.content["text"]
})
routing.py
from channels.routing import route
from channel_test.consumers import ws_message
channel_routing = [
route("websocket.recieve", ws_message),
]
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"channels",
"channel_test",
"argent_display"
]
CHANNEL_LAYERS = {
"default":{
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "argent_display.routing.channel_routing"
}
}
The django dev server is then run (manage.py runserver) and the following executed via the browser console:
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
console.log('test');
alert(e.data);
}
socket.onopen = function() {
socket.send("hello world");
}
Upon receiving a message, an alert should be given and the console logged to. However, neither occurs.
您应该将ws_message
使用者连接到websocket.receive
而不是websocket.recieve
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.