-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
patx
committed
Jan 20, 2025
1 parent
efba00e
commit babb9d7
Showing
6 changed files
with
92 additions
and
197 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
examples/pastebin-app/pastebin.py → examples/pastebin-app/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file was deleted.
Oops, something went wrong.