【即时通讯】轮询方式实现

技术栈

实现功能

  • 首次访问页面并发送消息时需要设置昵称
  • 发送内容为空时要提示用户不能发送空消息
  • 前端定时获取消息,然后展示在页面上。

效果展示

  • 首次发送需要设置昵称
  • 发送消息与消息展示
  • 提示用户不能发送空消息

后端接口

  • 发送消息
python 复制代码
DB = []
@router.post("/poll/send/message")
def send_message_api(request: WSGIRequest, message: MessageSchema) -> JsonResponse:
    """
    轮询通讯接收消息接口
    :param request: WSGIRequest
    :param message: MessageSchema
    :return: JsonResponse
    """
    message_info = message.dict()
    DB.append(message_info)
    return JsonResponse({"ok": True})
  • 获取消息
python 复制代码
@router.get("/poll/get/messages")
def get_messages_api(request: WSGIRequest, startIndex: int) -> JsonResponse:
    """
    轮询通讯获取消息接口
    :param request: WSGIRequest
    :param startIndex: int 上次获取聊天记录的最后位置
    """
    context = {
        "data": DB[startIndex:],
        "maxIndex": len(DB)
    }
    return JsonResponse(context)
相关推荐
B站_计算机毕业设计之家8 小时前
python汽车数据分析可视化系统 爬虫 懂车帝 毕业设计 Django框架 vue框架 大数据✅
爬虫·python·数据分析·django·汽车·推荐算法·懂车帝
yzx99101318 小时前
数据库完整指南:从基础到 Django 集成
数据库·python·django
蓑笠翁0011 天前
从零开始学习Python Django:从环境搭建到第一个 Web 应用
python·学习·django
yzx9910132 天前
Django 配置与安装完整指南
数据库·django·sqlite
m0_741585354 天前
Django开发环境
python·django
weixin_446260854 天前
Django - 让开发变得简单高效的Web框架
前端·数据库·django
闲人编程5 天前
使用Django从零开始构建一个个人博客系统
后端·python·django·接口·restful·web·个人博客
fangeqin5 天前
解决django.db.utils.OperationalError: attempt to write a readonly database错误
django·apache·sqlite3·selinux
做运维的阿瑞5 天前
从入门到精通:Django的深度探索之旅
开发语言·后端·python·系统架构·django
计算机毕设残哥6 天前
基于Hadoop+Spark的商店购物趋势分析与可视化系统技术实现
大数据·hadoop·python·scrapy·spark·django·dash