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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
小白勇闯网安圈8 小时前
Django 模板复用、ORM 查询与多对多关系
数据库·python·django
vx-程序开发12 小时前
django汽车租赁系统---附源码25360
java·javascript·spring boot·python·eclipse·django·php
SelectDB技术团队1 天前
阶跃星辰 Agent 可观测:Apache Doris / SelectDB 的技术能力与实践
django·apache·知识图谱
2401_868534782 天前
MATLAB:车牌识别
python·django
独码侠3 天前
Dify 知识库索引(中):打开源码,拆穿关于“经济索引”的三个谣言
后端·python·django·embedding·知识库·索引·dify
叫我王富贵i3 天前
0基础学Django
后端·python·django
2401_868534785 天前
综合脚本实战!自动备份、清日志、服务器巡检
python·django
weixin_BYSJ19875 天前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django
vx-程序开发5 天前
django医院预约挂号系统---附源码23353
java·javascript·spring boot·python·eclipse·django·php
weixin_BYSJ19876 天前
springboot医疗信息管理系统---附源码17465
java·javascript·spring boot·python·django·flask·php