Tuesday, September 5, 2017

broadcast messages to all connected clients in Django websockets

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]

chat.js:

    $(window).load(function() {      $messages.mCustomScrollbar();     var ws_path = "/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:

import json  from channels import Channel , Group  from channels.auth import http_session_user, channel_session_user, channel_session_user_from_http  from channels.sessions import channel_session    def ws_connect(message):       message.reply_channel.send({"accept": True})       Group('chat').add(message.reply_channel)  def ws_receive(message):      Group('chat').send({"text": message.content['text'],})  def ws_disconnect(message):     Group('chat').discard(message.reply_channel)

routing.py:

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

settings.py:

CHANNEL_LAYERS = {      "default": {          "BACKEND": "asgi_redis.RedisChannelLayer",           "CONFIG": {              "hosts": ['unix:///home/mansard/webapps/gadgetron/var/redis.sock',],},          "ROUTING": "chatbot.routing.channel_routing",      },  }

cht.html:

{% load static from staticfiles %}  <!DOCTYPE html>  <html >  <head>    <meta charset="UTF-8">    <title>المساعد الذكي</title>    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">    <link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans'>  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.3/jquery.mCustomScrollbar.min.css'>    <link rel="icon" href="/static/icon.png">    <link rel="favicon.ico" href="/static/icon.png">   <link rel="stylesheet" href="/static/css/chatbot_style.css">  </head>  <body>  <div class="chat">    <div class="chat-title">      <h1>محمد .. online now</h1>      <h2>الـمـسـاعـد الـذكـي</h2>      <figure class="avatar">        <img src="/static/icon.png" /></figure>    </div>    <div class="messages">      <div class="messages-content"></div>    </div>    <div class="message-box">      <textarea type="text" class="message-input" placeholder="اكتب سؤالك هنا"></textarea>      <button type="submit" class="message-submit">ارسال</button>    </div>    </div>  <div class="bg"></div>    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>  <script src='https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.3/jquery.mCustomScrollbar.concat.min.js'></script>  <script src="/static/js/channels/js/websocketbridge.js"></script>  <script src="/static/js/PP_chat_index.js"></script>


i tried echoing the messages back to me and it work perfectly. Now i moved on creating a chat server that adds a new client to the chat group and broadcast any incoming messages to all connected clients , i have configure and installed everything but the Group() doesn't work and nothing is happing and no error have been shown so i don't know where is the problem?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4e443a01-2098-44ae-a7f7-273f93458781%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment