Django和websocket实现简单的多人聊天

1、版本及环境 python 为3.9.12, django为4.2详细信息如下:

复制代码
asgiref           3.8.1
attrs             24.2.0
autobahn          24.4.2
Automat           24.8.1
cffi              1.17.1
channels          3.0.3
constantly        23.10.4
cryptography      44.0.0
daphne            3.0.2
Django            4.2
hyperlink         21.0.0
sqlparse          0.5.3
tomli             2.2.1
Twisted           24.11.0
txaio             23.1.1
typing_extensions 4.12.2
tzdata            2024.2
zope.interface    7.2

Django安装指定版本(上边的安装包也有可能安装多了,但必须要有python,Django, channels)

复制代码
pip install django==4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

2、创建Django项目

复制代码
django-admin startproject ws_demo
python manage.py startapp app01

3、安装channels

复制代码
pip install channels==3.0.3 -i  -i https://pypi.tuna.tsinghua.edu.cn/simple

4、项目整体目录结构如下:

5、配置setting文件

复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "channels",   # 添加channels
    "app01.apps.App01Config"   # 添加app01
]

# 在setting最后添加一个 "ws_demo.asgi.application" 意思是"项目名称.asgi文件.application变量名称"
ASGI_APPLICATION = "ws_demo.asgi.application"

6、asgi.py文件中的内容如下;

复制代码
import os
from channels.routing import ProtocolTypeRouter, URLRouter
# from django.conf.urls import url
# from chat import consumers
from django.core.asgi import get_asgi_application
from . import routings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ws_demo.settings')
# application = get_asgi_application()
application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    'websocket': URLRouter(routings.websocket_urlpatterns)
})

7、新建routings.py文件,其内容如下:

复制代码
from django.urls import re_path
from app01 import consumers

websocket_urlpatterns = [
    # 示例 url : xxxxx/room/x1/
    re_path(r"room/(?P<group>\w+)/$", consumers.ChatConsumer.as_asgi())
]

8、ws_demo下的urls.py中的内容如下:

复制代码
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    # path("admin/", admin.site.urls),
    path('index/', views.index),
]

9、在app01应用下新建templates目录,并在其下新建index.html,其内容如下:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .message {
            height: 300px;
            border: 1px solid #dddddd;
            width: 100%;
        }
    </style>
</head>

<body>
<div class="message" id="message"></div>
<div>
    <input type="text" placeholder="请输入" id="txt">
    <input type="button" value="发送" onclick="sendMsg()">
    <input type="button" value="关闭连接" onclick="closeConn()">
    <input type="button" value="重新建立连接" onclick="connect()">
</div>

<script>
    var socket = null;
    function connect() {
        socket = new WebSocket("ws:/127.0.0.1:8000/room/123/");

        // 连接完成以后 客户端自动触发
        socket.onopen = function (event) {
            let tag = document.createElement("div")
            tag.innerHTML = "连接已建立"
            document.getElementById("message").appendChild(tag)
        }

        // 回调函数 当服务端有消息发送到时,自动触发
        socket.onmessage = function (event) {
            console.log(event.data);
            let tag = document.createElement("div");
            tag.innerText = event.data;
            document.getElementById("message").appendChild(tag)
        }

        // 当连接关闭时,触发
        socket.onclose = function (event) {
            let tag = document.createElement("div")
            tag.innerHTML = "连接已断开"
            document.getElementById("message").appendChild(tag)

        }
    }
    function sendMsg() {
        let tag = document.getElementById("txt")
        socket.send(tag.value)
    }
    function closeConn() {
        // 向服务端发送断开连接
        socket.close();
    }
    connect()
</script>
</body>
</html>

10、在app01应用下新建consumers.py文件其内容如下:

py 复制代码
from channels.generic.websocket import WebsocketConsumer
from channels.exceptions import StopConsumer

#  简单版群聊就是定义个数组,并在连接时把对象添加到数组内,在断开链接时移除对象
CONN_LIST = []

class ChatConsumer(WebsocketConsumer):  # 继承WebsocketConsumer
   def websocket_connect(self, message):
       print("有人进行连接了。。。。")
       CONN_LIST.append(self)
       # 有客户端向后端发送 WebSocket 连接的请求时,自动触发(握手)
       self.accept()

   def websocket_receive(self, message):
       # 浏览器基于 WebSocket 向后端发送数据,自动触发接收消息
       print(message)
       #  一对一聊天
       # if message["text"] == "close":
       #     self.send("服务端主动关闭连接")
       #     #  服务端主动关闭连接
       #     self.close()
       # self.send(message["text"])
       for conn in CONN_LIST:
           conn.send(message["text"])



   def websocket_disconnect(self, message):
       # 客户端向服务端断开连接时,自动触发
       CONN_LIST.remove(self)
       print("连接断开!!")
       raise StopConsumer()

11、运行项目 在项目根目录下执行:

复制代码
python manage.py runserver

12、至此即可在浏览器中进行简单的多人聊天,结果如下:


相关推荐
rannn_1116 分钟前
【苍穹外卖|Day10】Spring Task、订单状态定时处理、WebSocket、来单提醒、客户催单
java·后端·websocket·苍穹外卖
wx_bishe2881 天前
python社区流浪动物猫狗救助救援网站_4a4i2--论文_pycharm django vue flask
python·pycharm·django·毕业设计
曲幽2 天前
FastAPI实战:WebSocket长连接保持与心跳机制,从入门到填坑
javascript·python·websocket·keep-alive·fastapi·heartbeat·connection
喵手2 天前
Python爬虫实战:数据质量检测与治理 - 构建健壮的爬虫数据管道(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·零基础python爬虫教学·数据质量检测与治理·爬虫数据管道
倚肆2 天前
WebSocket 完整教程:从安装到实战
java·websocket
倚肆2 天前
Spring WebSocket 的 MessageBrokerRegistry 与 StompEndpointRegistry 配置参数详解
java·websocket
秋氘渔2 天前
Django查询集优化:惰性加载、N+1查询问题及解决方案
mysql·django·select_related
喵手3 天前
Python爬虫实战:电商问答语料构建完整实战 - 从爬取到检索语料的工程化实现(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·零基础python爬虫教学·电商问答语料构建·爬取到检索语料
yq1982043011563 天前
基于Python爬虫原理的Pinterest视频资源获取技术解析与工具实践
爬虫·python·django·音视频
程序员敲代码吗3 天前
WebSocket在现代通讯系统中的应用与优势解析
网络·websocket·网络协议