Django频道 - 自定义路由似乎不工作

我正在理解Django的频道包,并希望尝试和更灵活,当涉及到可以在同一页上做不同的事情。我被困在试图找出为什么我的webSocketBridge不工作,因为它看起来应该看看其他例子。Django频道 - 自定义路由似乎不工作

这里是应用路由:

channel_routing = [ 

route('websocket.connect', ws_connect),

route('websocket.disconnect', ws_disconnect),

route('websocket.receive', ws_receive),

]

custom_routing = [

route("chat.receive", receive_chat_message, command="^send$"),

]

的settings.py读取主路由:

channel_routing = [  

include("ChatApp.routing.channel_routing", path=r"^/chat/stream/$"),

include("ChatApp.routing.custom_routing"),

]

消费者,而不是它甚至被称为:

@channel_session_user 

def receive_chat_message(message):

log.debug("ws recieved a message")

try:

data = json.loads(message['text'])

except ValueError:

log.debug("ws message isn't json text")

return

if 'message' not in data:

log.debug("ws message unexpected format data=%s", data)

return

if data:

room = Room.objects.first()

log.debug('chat message handle=%s message=%s', message.user, data['message'])

reply = Message.objects.create(

room=room,

handle=message.user.username,

message=data['message'],

)

Group('users').send({

'text': json.dumps({

'reply': reply.message,

'handle': reply.handle,

'timestamp': reply.formatted_timestamp

})

})

然后就是目前的JS绑定到这一切:

webSockedBridge.listen()里面

$(function() { 

// Correctly decide between ws:// and wss://

let ws_path = "/chat/stream/";

console.log("Connecting to " + ws_path);

let webSocketBridge = new channels.WebSocketBridge();

webSocketBridge.connect(ws_path);

webSocketBridge.listen(function(data) {

if (data.username) {

const username = encodeURI(data['username']);

const user = $('li').filter(function() {

return $(this).data('username') === username;

});

if (data['is_logged_in']) {

user.html(username + ': Online');

}

else {

user.html(username + ': Offline');

}

}

});

$("#chatform").on("submit", function(event) {

event.preventDefault();

const $message = $('#message');

const message = {

'command': 'send',

'message': $message.val()

};

console.log(message);

webSocketBridge.send(JSON.stringify(message));

$message.val('').focus();

return false;

});

// Helpful debugging

webSocketBridge.socket.onopen = function() {

console.log("Connected to chat socket");

};

webSocketBridge.socket.onclose = function() {

console.log("Disconnected from chat socket");

}

});

一切似乎做什么它应该,调用ws_connectws_disconnect。但是在#chatform上发生的那部分用命令提交的东西似乎不适用于我。

现在它只是调用route('websocket.receive', ws_receive)而不是自定义路由。为了使用该命令而缺少什么?

回答:

有一些问题与您的路由:年底

include("ChatApp.routing.channel_routing", path=r"^/chat/stream/$") 

删除$。

这是我的项目结构:

Project/ 

project/

...

routing.py

settings.py

app/

...

routing.py

在settings.py

CHANNEL_LAYERS = { 

'default': {

'BACKEND' : 'asgi_redis.RedisChannelLayer',

'CONFIG': {

'hosts': [('localhost', 6379)],

},

'ROUTING': 'project.routing.channel_routing'

}

}

项目/工程/ routing.py

from channels import include 

channel_routing = [

include('app.routing.channel_routing', path=r'^/chat/stream/'),

include('app.routing.custom_routing'),

]

项目/应用/ routing.py

from channels import route 

from .consumers import (ws_connect,

ws_receive,

ws_disconnect,

receive_chat_message,

another_command_chat_message,

)

channel_routing = [

route('websocket.connect', ws_connect),

route('websocket.receive', ws_receive),

route('websocket.disconnect', ws_disconnect),

]

custom_routing = [

route('chat.receive', receive_chat_message, command="^send$"),

route('chat.receive', another_command_chat_message, command="^cmd$"),

]

consumers.py

from channels import Channel 

import json

def ws_receive(message):

print('Message received')

message.reply_channel.send({'text':message.content['text']})

print('Message.content:', message.content)

print(type(message.content))

print('Message[text]:', message['text'])

print(type(message['text']))

payload = json.loads(message['text'])

payload['reply_channel'] = message.content['reply_channel']

# receive the message and send to chat.receive channel

# with command as a filter

Channel('chat.receive').send(payload)

def ws_connect(message):

print('Connection estabslihed')

print(message.content)

message.reply_channel.send({'accept': True})

def ws_disconnect(message):

print('Disconnecting')

def receive_chat_message(message):

print('chat receive message with `send` command')

print(message)

print(message.content)

def another_command_chat_message(message):

print('chat another command with `cmd` ')

print(message)

print(message.content)

以上是 Django频道 - 自定义路由似乎不工作 的全部内容, 来源链接: utcz.com/qa/258028.html

回到顶部