Skip to content

Instantly share code, notes, and snippets.

@ffd8
Created November 15, 2023 22:01
Show Gist options
  • Save ffd8/27b3d79e1ea7d75c07a6ce48e2ffc2aa to your computer and use it in GitHub Desktop.
Save ffd8/27b3d79e1ea7d75c07a6ce48e2ffc2aa to your computer and use it in GitHub Desktop.
Websockets - Processing websocket server, web browser client
<!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>
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