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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
开开心心就好3 小时前
内存清理工具开源免费,自动优化清理项
linux·运维·服务器·python·django·pdf·1024程序员节
开开心心_Every3 小时前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别
一代土怪4 小时前
django中设置中国时区
python·django
xj7573065337 小时前
《精通Django》 第7章 高级视图和URL配置
数据库·django·sqlite
@zulnger1 天前
Django 框架(模板)
笔记·python·学习·django
python_王子1 天前
python_django_跨境电商产品推荐与展示系统[结尾附下载地址]
python·信息可视化·django
开开心心_Every1 天前
免费AI图片生成工具:输入文字直接出图
服务器·前端·python·学习·edge·django·powerpoint
内存不泄露1 天前
基于Django和Vue的MOOC在线选课学习系统设计与实现
vue.js·学习·django
内存不泄露2 天前
二手物品交易平台
spring boot·小程序·django
luoluoal2 天前
基于python的基于深度学习的车俩特征分析系(源码+文档)
python·mysql·django·毕业设计·源码