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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
Red丶哞4 小时前
[Django Message超全总结教程](武沛齐老师)
数据库·django·sqlite
曲幽5 小时前
Django入门指南:Python Web开发的“瑞士军刀”
python·django·flask·fastapi·web·pythonweb
一代土怪7 小时前
django中实时更新数据库
python·django
@zulnger9 小时前
Django 模型
后端·python·django
计算机毕业编程指导师9 小时前
【计算机毕设选题】基于Spark的拉勾网招聘数据分析系统源码,Python+Django全流程
大数据·hadoop·python·spark·django·招聘·拉勾网
xj7573065339 小时前
Django 面试常见问题
python·面试·django
QQ4022054961 天前
python基于vue的大学生课堂考勤系统设计与实现django flask pycharm
vue.js·python·django
WangYaolove13141 天前
基于自适应svm电影评价倾向性分析(源码+文档)
python·django·毕业设计·源码
开开心心就好1 天前
内存清理工具开源免费,自动优化清理项
linux·运维·服务器·python·django·pdf·1024程序员节
开开心心_Every1 天前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别