Skip to content

Commit 72764b2

Browse files
WebSocket Series files
1 parent 1ee1543 commit 72764b2

12 files changed

Lines changed: 93252 additions & 0 deletions

File tree

Misc/WebSockets/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Before running the Nodejs scripts, remember to install dependencies for each project by opening a terminal in each respective folder, and install dependencies typing `npm i`.
3+
4+
Links to online projects:
5+
- https://glitch.com/edit/#!/simple-websocket-server-echo
6+
- https://editor.p5js.org/garciadelcastillo/sketches/mNWIaLhMQ
7+
- https://glitch.com/edit/#!/sync-chasing-ball
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
const WebSocket = require('ws');
3+
4+
// const serverAddress = "ws://127.0.0.1:5000";
5+
const serverAddress = 'wss://simple-websocket-server-echo.glitch.me/';
6+
7+
const ws = new WebSocket(serverAddress, {
8+
headers: {
9+
"user-agent": "Mozilla"
10+
}
11+
});
12+
13+
ws.on('open', function() {
14+
ws.send("Hello from PCamp!");
15+
});
16+
17+
ws.on('message', function(msg) {
18+
console.log("Received msg from the server: " + msg);
19+
});
20+

Misc/WebSockets/client-simple/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "client-simple",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "client.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"ws": "^7.4.3"
13+
}
14+
}

Misc/WebSockets/server-echo/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "server-echo",
3+
"version": "1.0.0",
4+
"description": "Some nice text here",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"ws": "^7.4.3"
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
const WebSocket = require('ws');
3+
4+
const PORT = 5000;
5+
6+
const wsServer = new WebSocket.Server({
7+
port: PORT
8+
});
9+
10+
wsServer.on('connection', function (socket) {
11+
// Some feedback on the console
12+
console.log("A client just connected");
13+
14+
// Attach some behavior to the incoming socket
15+
socket.on('message', function (msg) {
16+
console.log("Received message from client: " + msg);
17+
// socket.send("Take this back: " + msg);
18+
19+
// Broadcast that message to all connected clients
20+
wsServer.clients.forEach(function (client) {
21+
client.send("Someone said: " + msg);
22+
});
23+
24+
});
25+
26+
});
27+
28+
console.log( (new Date()) + " Server is listening on port " + PORT);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html><html lang="en"><head>
2+
<script src="p5.js"></script>
3+
<script src="p5.sound.min.js"></script>
4+
<link rel="stylesheet" type="text/css" href="style.css">
5+
<meta charset="utf-8">
6+
7+
</head>
8+
<body>
9+
<script src="sketch.js"></script>
10+
11+
12+
</body></html>

0 commit comments

Comments
 (0)