Skip to content

Commit

Permalink
updated the websockets example
Browse files Browse the repository at this point in the history
  • Loading branch information
patx committed Jan 20, 2025
1 parent efba00e commit babb9d7
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 197 deletions.
171 changes: 0 additions & 171 deletions .gitignore

This file was deleted.

32 changes: 30 additions & 2 deletions MicroPie.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
"""
MicroPie: A simple and lightweight Python micro web framework w/ WSGI support
https://patx.github.io/micropie
MicroPie: A simple Python ultra-micro web framework with WSGI
support. https://patx.github.io/micropie
Copyright Harrison Erd
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import http.server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""circuits-pastebin
"""
A simple no frills pastebin using MicroPie, pickleDB, and pygments.
"""

Expand Down
2 changes: 1 addition & 1 deletion examples/todo-app/todo.py → examples/todo-app/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
A ToDo Application using the MicroPie framework and KenobiDB.
A ToDo List Example Application using the MicroPie framework and pickleDB.
"""

import os
Expand Down
60 changes: 60 additions & 0 deletions examples/websockets-chat/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import asyncio
import websockets
from MicroPie import Server

class MyApp(Server):
def index(self):
return """
<html>
<head>
<script>
var ws = new WebSocket("ws://localhost:8765");
ws.onmessage = function(event) {
document.getElementById("output").innerHTML += event.data + "<br>";
};
function sendMessage() {
var message = document.getElementById("message").value;
ws.send(message);
}
</script>
</head>
<body>
<h1>WebSocket Chat</h1>
<input type="text" id="message" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
<div id="output"></div>
</body>
</html>
"""

async def websocket_handler(websocket):
"""Handles incoming WebSocket connections"""
print("Client connected")
try:
async for message in websocket:
print(f"Received: {message}")
response = f"Echo: {message}"
await websocket.send(response)
except websockets.exceptions.ConnectionClosed:
print("Client disconnected")

async def websocket_server():
"""Start WebSocket server within the asyncio event loop"""
async with websockets.serve(websocket_handler, "localhost", 8765):
print("WebSocket server started on ws://localhost:8765")
await asyncio.Future() # Keep the server running indefinitely

def start_websocket_server():
"""Runs the WebSocket server with asyncio.run() in a separate thread"""
asyncio.run(websocket_server())

if __name__ == "__main__":
import threading

# Start the WebSocket server in a separate thread
ws_thread = threading.Thread(target=start_websocket_server, daemon=True)
ws_thread.start()

# Start the MicroPie server
MyApp().run()

21 changes: 0 additions & 21 deletions examples/websockets/websockets.py

This file was deleted.

0 comments on commit babb9d7

Please sign in to comment.