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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
QD.Joker4 小时前
Django ORM 单表操作
数据库·django
gongzairen16 小时前
Ngrok 内网穿透实现Django+Vue部署
后端·python·django
快乐点吧1 天前
【代理错误 django】Request error: HTTPSConnectionPool(host=‘‘, port=443): 、
后端·python·django
KENYCHEN奉孝1 天前
一个基于Django的写字楼管理系统实现方案
数据库·python·django·sqlite
程序媛徐师姐2 天前
Python Django基于协同过滤算法的招聘信息推荐系统【附源码、文档说明】
python·django·协同过滤算法·招聘信息推荐系统·招聘信息·python招聘信息推荐系统·python招聘信息
橘猫云计算机设计2 天前
基于django云平台的求职智能分析系统(源码+lw+部署文档+讲解),源码可白嫖!
数据库·spring boot·后端·python·django·毕业设计
声声codeGrandMaster2 天前
Django之modelform使用
后端·python·django
松前卡气加超级漂3 天前
Django:高效构建现代Web应用的利器
前端·python·django
alicema11113 天前
Python-Django集成yolov识别模型摄像头人数监控网页前后端分离
开发语言·后端·python·算法·机器人·django
杨涣群3 天前
Django 开发服务器
python·django