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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
En^_^Joy1 天前
Django模型:数据库操作全指南
数据库·django·sqlite
__log2 天前
ComfyUI 集成技术方案分析报告
javascript·python·django
俊哥工具3 天前
鼠标自动连点怎么设置?详细教学,简单易懂!
python·django·pdf·计算机外设·virtualenv·pygame
源码之家4 天前
计算机毕业设计:Pyhon健康数据分析系统 Django框架 数据分析 可视化 身体数据分析 大数据(建议收藏)✅
大数据·python·数据挖掘·数据分析·django·lstm·课程设计
vx_biyesheji00044 天前
计算机毕业设计:Python医疗数据分析平台 Flask框架 数据分析 可视化 医疗大数据 用户画像(建议收藏)✅
大数据·python·深度学习·数据分析·django·flask·课程设计
源码之家4 天前
计算机毕业设计:Python医疗数据可视化系统 Flask框架 数据分析 可视化 医疗大数据 用户画像(建议收藏)✅
python·深度学习·信息可视化·数据分析·django·flask·课程设计
Wonderful U4 天前
【前后端】如何使用agent来实现django+vue的前后端开发
vue.js·django
QQ8057806515 天前
django基于机器学习的电商评论情感分析系统设计实现
python·机器学习·django
凯瑟琳.奥古斯特6 天前
Django Flask FastAPI 三者对比
开发语言·python·django·flask·fastapi
Betelgeuse767 天前
Django 中间件 4 大钩子 & CBV vs FBV 对比实战
python·中间件·django