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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
vx-程序开发2 小时前
springboot具备推荐和预警机制的大学生兼职平台的设计与实现-计算机毕业设计源码17157
java·c++·spring boot·python·spring·django·php
Mr数据杨2 小时前
【Dv3Admin】Django通用自定义工作台卡片
数据库·django·sqlite
AC赳赳老秦3 小时前
国产化AI运维新趋势:DeepSeek赋能国产算力部署的高效故障排查
大数据·人工智能·python·django·去中心化·ai-native·deepseek
wefly20173 小时前
告别繁琐配置!m3u8live.cn让 M3U8 链接验证变得如此简单
开发语言·前端·python·django·flask·开发工具
ding_zhikai1 天前
【Web应用开发笔记】Django笔记9:Django部署注意事项补充
笔记·后端·python·django
吃吃喝喝小朋友1 天前
Django Admin后台系统
后端·python·django
杰杰7981 天前
深入理解 Django REST Framework 的 Serializer(上)
后端·python·django
chushiyunen1 天前
django日志使用笔记
数据库·笔记·django
计算机徐师兄1 天前
Python基于Django的汉语文本阅读难度分级系统(附源码,文档说明)
python·机器学习·django·汉语文本阅读难度分级系统·python文本阅读难度分级·文本阅读难度分级系统·汉语文本阅读难度分级
Amctwd1 天前
【OS】操作系统的 I/O
django