# fastapi-websocket **Repository Path**: lch0821/fastapi-websocket ## Basic Information - **Project Name**: fastapi-websocket - **Description**: 基于 FastApi 的 Websocket 例子 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-03 - **Last Updated**: 2022-05-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 基于 FastApi 的 Websocket ## 1 快速开始 ### 1.1 Clone 代码 ```sh git clone https://gitee.com/lch0821/fastapi-websocket.git cd fastapi-websocket ``` ### 1.2 创建虚拟环境 ```sh python3 -m venv .env # MacOS / Linux / Windows Gitbash / WSL source .env/bin/activate pip install --upgrade pip ``` ### 1.3 安装依赖包 ```sh pip install -r requirements.txt ``` ### 1.4 启动程序 ```sh uvicorn server:app --host 0.0.0.0 --port 8080 ``` ### 1.5 测试 #### 1.5.1 连接 WebSocket 服务 1. 使用 `cURL` ```sh curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: 6U8Nvdbu4jFZKLw2f4dZ+g==" -H "Host: localhost" http://localhost:8080/ws ``` 2. 使用客户端 ```sh python client.py ``` 3. 使用 Postman 具体参考:[https://blog.postman.com/postman-supports-websocket-apis/](https://blog.postman.com/postman-supports-websocket-apis/)。 #### 1.5.2 发送消息: ```sh curl -X POST 'localhost:8080/msg' -H 'Content-Type: application/json' -d '{"msg": "text"}' ``` 也可以使用 Postman。