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

技术栈

实现功能

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

效果展示

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

后端接口

  • 发送消息
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)
相关推荐
qq_13948428827 小时前
基于Python网易云排行榜数据分析系统设计与实现
大数据·hadoop·python·django·flask
牢七7 小时前
热身热身热身热身。
django
努力努力再努力wz7 小时前
2025年度总结:不断迈出第一步
linux·运维·服务器·数据结构·redis·python·django
luoluoal1 天前
基于python的语音识别与蓝牙通信的温控系统(源码+文档)
python·mysql·django·毕业设计·源码
linuxxx1101 天前
request.build_absolute_uri()关于使用IP+端口
网络·python·网络协议·tcp/ip·django
linuxxx1101 天前
request.build_absolute_uri()用法
python·django
linuxxx1101 天前
request.build_absolute_uri()为什么没有获得端口?
python·nginx·django
㳺三才人子1 天前
初探 Python + Django
开发语言·python·django
java1234_小锋2 天前
[免费]基于Python的Django+Vue3在线商城系统(简易版)【论文+源码+SQL脚本】
python·django·商城系统·python毕业设计·在线商城
qq_13948428822 天前
python基于大数据技术的酒店消费数据分析系统
大数据·python·scrapy·django·flask