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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
DingYuan10110 小时前
Django模板继承详解
django
WangYaolove131410 小时前
基于python的漏洞扫描系统(源码+文档)
python·mysql·django·毕业设计·源码
上半场结束,中场已休息,下半场ing1 天前
111111111
django
ycydynq1 天前
django 数据库 多表操作
数据库·python·django
七夜zippoe2 天前
GraphQL在Python中的完整实现:从基础到企业级实战
django·graphql·schema·resolver·strawberry
ycydynq2 天前
django 数据库 单表操作
数据库·oracle·django
weixin_BYSJ19872 天前
django农作物批发交易系统--附源码24008
java·javascript·spring boot·python·django·flask·php
Edward-tan2 天前
【玩转全栈】----Django模板语法、请求与响应
后端·python·django
硕博计算机毕设指导2 天前
【大数据毕设全套源码+文档】Django基于大数据技术的智慧居家养老服务平的设计与实现(丰富项目+远程调试+讲解+定制)
大数据·python·信息可视化·django·毕业设计·课程设计
B站计算机毕业设计超人2 天前
计算机毕业设计Python+Django考研院校推荐系统 考研分数线预测系统 大数据毕业设计 (代码+LW文档+PPT+讲解视频)
大数据·人工智能·hive·python·django·毕业设计·课程设计