دنبال کننده ها

۱۳۹۶ شهریور ۱۳, دوشنبه

python - start send and receive messages in Django websockets

[ad_1]



a have a Django chat application using web sockets and channels,the wecbokect connection is establish as you can see here:



[mansard@web569 src]$ python3.5 manage.py runserver 22783 Performing system checks...

Django version 1.11, using settings 'chatbot.settings'
2017-09-04 16:53:05,478 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,479 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,479 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2017-09-04 16:53:05,480 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2017-09-04 16:53:05,480 - INFO - server - Using busy-loop synchronous mode on channel layer
2017-09-04 16:53:05,480 - INFO - server - Listening on endpoint tcp:port=22783:interface=127.0.0.1
[2017/09/04 16:54:09] WebSocket HANDSHAKING /webscok/ [127.0.0.1:42846]
[2017/09/04 16:54:09] WebSocket CONNECT /webscok/ [127.0.0.1:42846]


But , when i want to start chatting nothing is happening where the message should be received to the other user and the Error Console doesn’t show any error!?
I think it should be the IP address because http://127.0.0.1:22783/ and having the channel layer on in memory.



chat.js:



$(window).load(function() 
$messages.mCustomScrollbar();
var ws_path = "/Pchat/webscok/";
var socket = new WebSocket("ws://" + window.location.host + ws_path);
socket.onmessage = function(e)
chatMessage(e.data);
$('.message-submit').click(function()
msg = $('.message-input').val();
socket.send(msg);
insertMessage(msg););
$(window).on('keydown', function(e)
if (e.which == 13)
msg = $('.message-input').val();
socket.send(msg);
insertMessage(msg););
setTimeout(function()
welcomingMessage();, 100);
);


consumers.py:



@channel_session_user_from_http
def ws_connect(message):
message.reply_channel.send(
'accept': True)
@channel_session_user_from_http
def ws_receive(message):
message.reply_channel.send(
"text": message.content['text'],
)


routing.py:



from Pchat.consumers import ws_connect, ws_disconnect, ws_receive

channel_routing = [
route("websocket.connect", ws_connect,path=r"^/Pchat/webscok"),
route("websocket.disconnect", ws_disconnect),
route("websocket.receive", ws_receive),
]


settings.py:



CHANNEL_LAYERS = 
"default":
"BACKEND": "asgiref.inmemory.ChannelLayer",
"CONFIG":
"hosts": ['unix:///home/mansard/webapps/gadgetron/var/redis.sock',],,
"ROUTING": "chatbot.routing.channel_routing",
,




[ad_2]

لینک منبع