-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace main with development branch. MICEOPIE IS NOW ASGI BASED, RUN…
… METHOD GONE
- Loading branch information
patx
committed
Jan 28, 2025
1 parent
ccaa19b
commit 2519a30
Showing
20 changed files
with
826 additions
and
757 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,11 @@ | ||
from MicroPie import Server | ||
|
||
|
||
class Root(Server): | ||
|
||
def index(self, name=None): | ||
if name: | ||
return f'Hello {name}' | ||
return 'Hello ASGI World!' | ||
|
||
app = Root() # Run with `uvicorn app:app` |
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 socketio | ||
from MicroPie import Server | ||
|
||
# Create a Socket.IO server with CORS support | ||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*") # Allow all origins | ||
|
||
# Create the MicroPie server | ||
class MyApp(Server): | ||
def index(self): | ||
return """ | ||
<html> | ||
<head> | ||
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script> | ||
<script> | ||
var socket = io("http://localhost:8000"); | ||
socket.on("connect", function() { | ||
console.log("Connected to Socket.IO server"); | ||
}); | ||
socket.on("message", function(data) { | ||
document.getElementById("output").innerHTML += data + "<br>"; | ||
}); | ||
function sendMessage() { | ||
var message = document.getElementById("message").value; | ||
socket.send(message); | ||
document.getElementById("message").value = ""; // Clear input after sending | ||
} | ||
window.onbeforeunload = function() { | ||
socket.disconnect(); | ||
}; | ||
</script> | ||
</head> | ||
<body> | ||
<h1>Socket.IO Chat</h1> | ||
<input type="text" id="message" placeholder="Type a message"> | ||
<button onclick="sendMessage()">Send</button> | ||
<div id="output"></div> | ||
</body> | ||
</html> | ||
""" | ||
|
||
# Socket.IO event handlers | ||
@sio.event | ||
async def connect(sid, environ): | ||
print(f"Client connected: {sid}") | ||
|
||
@sio.event | ||
async def disconnect(sid): | ||
print(f"Client disconnected: {sid}") | ||
|
||
@sio.event | ||
async def message(sid, data): | ||
print(f"Received message from {sid}: {data}") | ||
# Broadcast the message to all connected clients | ||
await sio.emit("message", f"User: {data}", room=None) | ||
|
||
|
||
|
||
# Attach Socket.IO to the ASGI app | ||
app = MyApp() | ||
asgi_app = socketio.ASGIApp(sio, app) |
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,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Webcam Streaming</title> | ||
</head> | ||
<body> | ||
<h1>Webcam Streaming App</h1> | ||
<form method="post" action="/submit"> | ||
<input type="text" name="username" placeholder="Enter username" required> | ||
<button type="submit" name="action" value="Start Streaming">Start Streaming</button> | ||
<button type="submit" name="action" value="Watch Stream">Watch Stream</button> | ||
</form> | ||
</body> | ||
</html> | ||
|
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,57 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script> | ||
<title>Streaming: {{ username }}</title> | ||
</head> | ||
<body> | ||
<h1>Streaming as {{ username }}</h1> | ||
<video id="webcam" autoplay playsinline></video> | ||
|
||
<script> | ||
const username = "{{ username }}"; | ||
const socket = io(); | ||
|
||
// Join the room as a streamer | ||
socket.emit("join_room", { username }); | ||
|
||
socket.on("connect", () => { | ||
console.log("Connected as streamer for", username); | ||
startWebcam(); | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
console.log("Disconnected"); | ||
}); | ||
|
||
async function startWebcam() { | ||
try { | ||
const stream = await navigator.mediaDevices.getUserMedia({ video: true }); | ||
const videoElement = document.getElementById("webcam"); | ||
videoElement.srcObject = stream; | ||
|
||
const canvas = document.createElement("canvas"); | ||
const context = canvas.getContext("2d"); | ||
const track = stream.getVideoTracks()[0]; | ||
const settings = track.getSettings(); | ||
canvas.width = settings.width || 640; | ||
canvas.height = settings.height || 480; | ||
|
||
setInterval(() => { | ||
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height); | ||
const frameDataUrl = canvas.toDataURL("image/webp"); | ||
|
||
// Send frame to server | ||
socket.emit("stream_frame", { username, frame: frameDataUrl }); | ||
}, 100); | ||
} catch (err) { | ||
console.error("Error accessing webcam:", err); | ||
} | ||
} | ||
</script> | ||
|
||
</body> | ||
</html> | ||
|
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,37 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script> | ||
<title>Watching: {{ username }}</title> | ||
</head> | ||
<body> | ||
<h1>Watching Stream of {{ username }}</h1> | ||
<img id="videoFeed" style="width: 100%; max-width: 800px;" /> | ||
|
||
|
||
<script> | ||
const username = "{{ username }}"; | ||
const socket = io(); | ||
|
||
// Join the room as a watcher | ||
socket.emit("join_room", { username }); | ||
|
||
socket.on("connect", () => { | ||
console.log("Connected as watcher for", username); | ||
}); | ||
|
||
socket.on("video_frame", (data) => { | ||
if (data.username === username) { | ||
document.getElementById("videoFeed").src = data.frame; | ||
} | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
console.log("Disconnected"); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.