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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
言乐66 小时前
Python游戏水平测试辅助系统2
开发语言·windows·python·游戏·django
vx-程序开发12 小时前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
言乐616 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
小白勇闯网安圈1 天前
Django 响应对象、文件上传与类视图
数据库·django·sqlite
小白勇闯网安圈1 天前
Django 模板复用、ORM 查询与多对多关系
数据库·python·django
vx-程序开发1 天前
django汽车租赁系统---附源码25360
java·javascript·spring boot·python·eclipse·django·php
SelectDB技术团队2 天前
阶跃星辰 Agent 可观测:Apache Doris / SelectDB 的技术能力与实践
django·apache·知识图谱
2401_868534783 天前
MATLAB:车牌识别
python·django
独码侠4 天前
Dify 知识库索引(中):打开源码,拆穿关于“经济索引”的三个谣言
后端·python·django·embedding·知识库·索引·dify
叫我王富贵i4 天前
0基础学Django
后端·python·django