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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
千层冷面1 天前
Flask ORM 查询详解:Model.query vs db.session.query vs db.session.execute
数据库·python·django·flask
王小王-1232 天前
基于Django的福建省旅游数据分析与可视化系统【城市可换】
数据分析·django·旅游·携程·福建省旅游可视化·旅游数据分析系统·景区数据分析
合作小小程序员小小店2 天前
web网站开发,在线%射击比赛成绩管理%系统开发demo,基于html,css,jquery,python,django,model,orm,mysql数据库
python·mysql·django·jquery·html5
Q_Q19632884752 天前
python基于Hadoop的超市数据分析系统
开发语言·hadoop·spring boot·python·django·flask·node.js
Q_Q5110082853 天前
python的滑雪场雪具租赁服务数据可视化分析系统
spring boot·python·信息可视化·django·flask·node.js·php
noravinsc3 天前
django 如何读取项目根目录下的文件内容
后端·python·django
合作小小程序员小小店3 天前
web安全开发,在线%射击比赛管理%系统开发demo,基于html,css,jquery,python,django,三层mysql数据库
css·mysql·django·html·jquery
桃源学社(接毕设)4 天前
基于Django珠宝购物系统设计与实现(LW+源码+讲解+部署)
人工智能·后端·python·django·毕业设计
alexander0685 天前
Django路由学习笔记
笔记·学习·django
木有童年5 天前
Django配置sqllite之外的数据库
django