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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
wx_bishe2881 天前
python社区流浪动物猫狗救助救援网站_4a4i2--论文_pycharm django vue flask
python·pycharm·django·毕业设计
秋氘渔2 天前
Django查询集优化:惰性加载、N+1查询问题及解决方案
mysql·django·select_related
yq1982043011563 天前
基于Python爬虫原理的Pinterest视频资源获取技术解析与工具实践
爬虫·python·django·音视频
feasibility.4 天前
打造AI+准SaaS:中文法律检索分析平台
vue.js·人工智能·自然语言处理·django·sass·web·法律
涛涛讲AI4 天前
Django Admin模块的都有哪些表以及之间的关系
数据库·django·sqlite
Sheffield6 天前
为什么Django这么慢,却还是Python后端第一梯队呢?
linux·python·django
海兰6 天前
Elasticsearch 搜索方案与技术栈深度解析
大数据·elasticsearch·django
yixin1237 天前
【玩转全栈】----Django基本配置和介绍
数据库·django·sqlite
luoluoal7 天前
基于python的医疗知识图谱问答系统(源码+文档)
python·mysql·django·毕业设计·源码
yueyin1234567 天前
在Django中安装、配置、使用CKEditor5,并将CKEditor5录入的文章展现出来,实现一个简单博客网站的功能
数据库·django·sqlite