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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
Mr数据杨12 小时前
【Codex】用班级设置模块维护教学组织基础数据
django·codex·项目开发
斯班奇的好朋友阿法法14 小时前
联调注意事项清单
django
forestqq15 小时前
基于openeuler2403sp3的容器,打包django运行环境镜像
后端·python·django
俊昭喜喜里21 小时前
Allegro/OrCad转换成立创eda文件
python·django
Mr数据杨1 天前
【Codex】用教材配置模块统一管理课程教材体系
django·codex·项目开发
Mr数据杨1 天前
【Codex】用APP绑定教程模块规范移动端接入指引
java·前端·javascript·django·codex·项目开发
阿豪只会阿巴2 天前
【没事学点啥】TurboBlog轻量级个人博客项目——项目介绍
javascript·python·django·html
Mr数据杨2 天前
【Codex】用PPT文案额外描述优化课件生成细节
java·javascript·django·powerpoint·codex·项目开发
Mr.朱鹏2 天前
5.LangChain零基础速通-LCEL链式调用
python·langchain·django·大模型·llm·virtualenv
斯班奇的好朋友阿法法2 天前
IP白名单 + 预共享密钥跳转服务
django