Metadata-Version: 2.4
Name: micropython-websocket_server
Version: 0.1.0
Summary: A WebSocket server library for MicroPython on ESP32
Author-email: 李一博 <slyb@tju.edu.cn>
License: MIT License
        
        Copyright (c) 2026 李一博
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://gitee.com/slyb2020/micropython-websocket_server
Keywords: micropython,esp32,websocket,websocket-server,iot,embedded
Classifier: Programming Language :: Python :: Implementation :: MicroPython
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: System :: Networking
Classifier: Intended Audience :: Developers
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# micropython-websocket_server

适用于ESP32的MicroPython WebSocket-server模块。

## 安装方法

在Thonny中安装到ESP32：

1. 用USB将ESP32连接到电脑
2. 打开Thonny，右下角切换为 MicroPython(ESP32)
3. 菜单 Tools → Manage packages
4. 搜索 `micropython-websocket_server`
5. 点击安装

## 使用示例

```python
import network
import micropython_websocket_server as websocket_server

def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print("连接 Wi-Fi...")
        wlan.connect("你的WiFi名称", "你的WiFi密码")
        while not wlan.isconnected():
            pass
    ip = wlan.ifconfig()[0]
    print("Wi-Fi 已连接，ESP32 IP 地址:", ip)
    print("请在 PC 端连接: ws://{}:8765".format(ip))
    return ip

do_connect()

# 创建并启动服务器
server = websocket_server.WebSocketServer("0.0.0.0", 8765)
server.start()
print("WebSocket 服务器已启动，等待连接...")

try:
    while True:
        # 阻塞等待一个客户端连接
        conn, addr = server.accept()
        if conn is None:
            continue   # 握手失败，继续等待

        print("客户端已连接:", addr)

        # 与该客户端通信直到断开
        try:
            while True:
                msg = conn.recv()         # 阻塞接收
                print("收到:", msg)
                conn.send(msg)            # 原样返回
        except websocket_server.WebSocketConnectionClosedException:
            print("客户端已断开:", addr)
        finally:
            conn.close()
            server.remove_client(conn)

finally:
    server.stop()
```
