Created
November 15, 2023 22:01
-
-
Save ffd8/27b3d79e1ea7d75c07a6ce48e2ffc2aa to your computer and use it in GitHub Desktop.
Websockets - Processing websocket server, web browser client
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>websocket client</title> | |
<style> | |
body{ | |
background:#000; | |
color:#fff; | |
font-size:100pt; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="msg"></div> | |
<script> | |
// connect to websocket server | |
const exampleSocket = new WebSocket( | |
"ws://localhost:8025/blah" | |
); | |
// send keypress to websocket server | |
window.onkeydown = function(e){ | |
exampleSocket.send(e.key+""); | |
document.getElementById('msg').innerHTML = e.key | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
import websockets.*; | |
WebsocketServer ws; | |
String txt = ""; | |
void setup(){ | |
size(200,200); | |
// set websocket port, and path (optional) | |
ws= new WebsocketServer(this, 8025, "/blah"); | |
} | |
void draw(){ | |
background(0); | |
fill(255); | |
textSize(50); | |
textAlign(CENTER, CENTER); | |
text(txt, width/2, height/2); | |
} | |
void webSocketServerEvent(String msg){ | |
// process incoming message | |
txt = msg; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment