Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cloudbaserun/sse-demo/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
__pycache__
.pytest_cache
env
.DS_Store
21 changes: 21 additions & 0 deletions cloudbaserun/sse-demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

# 复制应用代码
COPY app.py .

# 暴露端口
EXPOSE 5000

# 设置环境变量
ENV FLASK_APP=app.py
ENV FLASK_ENV=production

# 启动命令
CMD ["python", "app.py"]
23 changes: 23 additions & 0 deletions cloudbaserun/sse-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# CloudBase 云托管 SSE 示例

## 命令行部署

在此目录下运行:

```sh
tcb cloudrun deploy -e your-env-id
```


## 控制台部署

使用文件夹上传方式选择此文件夹并上传,配置服务端口为 5000


## 测试访问

服务部署成功后,访问服务域名可以查看提示:服务器已启动,SSE 端点可通过 /stream 访问。

访问: 服务域名/stream 路径;可以查看到持续不断的内容输出;输出内容为服务端通过 SSE 协议持续返回到前端。


78 changes: 78 additions & 0 deletions cloudbaserun/sse-demo/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from flask import Flask, Response
import time
import json
import datetime
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}}) # 允许所有来源的跨域请求

@app.route('/stream')
def stream():
def event_stream():
count = 0
while True:
# 创建事件数据
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data = {
"count": count,
"time": current_time,
"message": f"这是第 {count} 条 SSE 消息"
}

# 按照 SSE 协议格式发送数据
yield f"id: {count}\n"
yield f"event: message\n"
yield f"data: {json.dumps(data, ensure_ascii=False)}\n\n"

count += 1
time.sleep(2) # 每2秒发送一次消息

# 设置响应头,指定内容类型为 text/event-stream
return Response(event_stream(), mimetype="text/event-stream")

@app.route('/')
def index():
# 返回简单的 HTML 页面,用于测试 SSE
return """
<!DOCTYPE html>
<html>
<head>
<title>Flask SSE 服务器</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
}
.info {
background-color: #f0f0f0;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
code {
background-color: #e0e0e0;
padding: 2px 4px;
border-radius: 3px;
}
</style>
</head>
<body>
<h1>Flask SSE 服务器</h1>
<div class="info">
<p>服务器已启动,SSE 端点可通过 <code>/stream</code> 访问。</p>
<p>客户端可以连接到 <code>http://localhost:5000/stream</code> 接收事件。</p>
</div>
</body>
</html>
"""

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
2 changes: 2 additions & 0 deletions cloudbaserun/sse-demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
flask-cors
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [📱 小程序](#-小程序)
- [🔄 跨端应用](#-跨端应用)
- [⚡ 函数型托管](#-函数型托管)
- [⚡ 云托管](#-云托管)
- [🤖 AI Agent 服务](#-ai-agent-服务)
- [🔗 MCP Server](#-mcp-server)
- [🔧 低代码应用](#-低代码应用)
Expand Down Expand Up @@ -94,6 +95,13 @@
- [全栈项目](./cloudrunfunctions/fullstack-project) - 云函数全栈项目示例
- [消息中心](./cloudrunfunctions/message-center) - 基于云函数的消息中心示例

### ⚡ 云托管

- [SSE 示例](./cloudbaserun/sse-demo) - 云托管使用 SSE 协议返回内容的示例
- [Spring Cloud 示例](./cloudbaserun/spring-cloud-docker-demo) - SpringCloud 构建微服务示例
- [云端开发环境](./cloudbaserun/deploy-code-server) - 云托管部署并运行云端开发环境示例


### 🤖 AI Agent 服务

- [DeepSeek Agent](./cloudrunfunctions/deepseek-agent) - DeepSeek 大模型 Agent 示例
Expand Down