summaryrefslogtreecommitdiffstats
path: root/testing/xpcshell/node-ws/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /testing/xpcshell/node-ws/examples
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/xpcshell/node-ws/examples')
-rw-r--r--testing/xpcshell/node-ws/examples/express-session-parse/index.js101
-rw-r--r--testing/xpcshell/node-ws/examples/express-session-parse/package.json11
-rw-r--r--testing/xpcshell/node-ws/examples/express-session-parse/public/app.js67
-rw-r--r--testing/xpcshell/node-ws/examples/express-session-parse/public/index.html24
-rw-r--r--testing/xpcshell/node-ws/examples/server-stats/index.js33
-rw-r--r--testing/xpcshell/node-ws/examples/server-stats/package.json9
-rw-r--r--testing/xpcshell/node-ws/examples/server-stats/public/index.html63
-rw-r--r--testing/xpcshell/node-ws/examples/ssl.js37
8 files changed, 345 insertions, 0 deletions
diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/index.js b/testing/xpcshell/node-ws/examples/express-session-parse/index.js
new file mode 100644
index 0000000000..b62a2e4a5f
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/express-session-parse/index.js
@@ -0,0 +1,101 @@
+'use strict';
+
+const session = require('express-session');
+const express = require('express');
+const http = require('http');
+const uuid = require('uuid');
+
+const { WebSocketServer } = require('../..');
+
+const app = express();
+const map = new Map();
+
+//
+// We need the same instance of the session parser in express and
+// WebSocket server.
+//
+const sessionParser = session({
+ saveUninitialized: false,
+ secret: '$eCuRiTy',
+ resave: false
+});
+
+//
+// Serve static files from the 'public' folder.
+//
+app.use(express.static('public'));
+app.use(sessionParser);
+
+app.post('/login', function (req, res) {
+ //
+ // "Log in" user and set userId to session.
+ //
+ const id = uuid.v4();
+
+ console.log(`Updating session for user ${id}`);
+ req.session.userId = id;
+ res.send({ result: 'OK', message: 'Session updated' });
+});
+
+app.delete('/logout', function (request, response) {
+ const ws = map.get(request.session.userId);
+
+ console.log('Destroying session');
+ request.session.destroy(function () {
+ if (ws) ws.close();
+
+ response.send({ result: 'OK', message: 'Session destroyed' });
+ });
+});
+
+//
+// Create an HTTP server.
+//
+const server = http.createServer(app);
+
+//
+// Create a WebSocket server completely detached from the HTTP server.
+//
+const wss = new WebSocketServer({ clientTracking: false, noServer: true });
+
+server.on('upgrade', function (request, socket, head) {
+ console.log('Parsing session from request...');
+
+ sessionParser(request, {}, () => {
+ if (!request.session.userId) {
+ socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
+ socket.destroy();
+ return;
+ }
+
+ console.log('Session is parsed!');
+
+ wss.handleUpgrade(request, socket, head, function (ws) {
+ wss.emit('connection', ws, request);
+ });
+ });
+});
+
+wss.on('connection', function (ws, request) {
+ const userId = request.session.userId;
+
+ map.set(userId, ws);
+
+ ws.on('message', function (message) {
+ //
+ // Here we can now use session parameters.
+ //
+ console.log(`Received message ${message} from user ${userId}`);
+ });
+
+ ws.on('close', function () {
+ map.delete(userId);
+ });
+});
+
+//
+// Start the server.
+//
+server.listen(8080, function () {
+ console.log('Listening on http://localhost:8080');
+});
diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/package.json b/testing/xpcshell/node-ws/examples/express-session-parse/package.json
new file mode 100644
index 0000000000..406706ce8a
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/express-session-parse/package.json
@@ -0,0 +1,11 @@
+{
+ "author": "",
+ "name": "express-session-parse",
+ "version": "0.0.0",
+ "repository": "websockets/ws",
+ "dependencies": {
+ "express": "^4.16.4",
+ "express-session": "^1.16.1",
+ "uuid": "^8.3.2"
+ }
+}
diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js b/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js
new file mode 100644
index 0000000000..f70dc21835
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js
@@ -0,0 +1,67 @@
+(function () {
+ const messages = document.querySelector('#messages');
+ const wsButton = document.querySelector('#wsButton');
+ const wsSendButton = document.querySelector('#wsSendButton');
+ const logout = document.querySelector('#logout');
+ const login = document.querySelector('#login');
+
+ function showMessage(message) {
+ messages.textContent += `\n${message}`;
+ messages.scrollTop = messages.scrollHeight;
+ }
+
+ function handleResponse(response) {
+ return response.ok
+ ? response.json().then((data) => JSON.stringify(data, null, 2))
+ : Promise.reject(new Error('Unexpected response'));
+ }
+
+ login.onclick = function () {
+ fetch('/login', { method: 'POST', credentials: 'same-origin' })
+ .then(handleResponse)
+ .then(showMessage)
+ .catch(function (err) {
+ showMessage(err.message);
+ });
+ };
+
+ logout.onclick = function () {
+ fetch('/logout', { method: 'DELETE', credentials: 'same-origin' })
+ .then(handleResponse)
+ .then(showMessage)
+ .catch(function (err) {
+ showMessage(err.message);
+ });
+ };
+
+ let ws;
+
+ wsButton.onclick = function () {
+ if (ws) {
+ ws.onerror = ws.onopen = ws.onclose = null;
+ ws.close();
+ }
+
+ ws = new WebSocket(`ws://${location.host}`);
+ ws.onerror = function () {
+ showMessage('WebSocket error');
+ };
+ ws.onopen = function () {
+ showMessage('WebSocket connection established');
+ };
+ ws.onclose = function () {
+ showMessage('WebSocket connection closed');
+ ws = null;
+ };
+ };
+
+ wsSendButton.onclick = function () {
+ if (!ws) {
+ showMessage('No WebSocket connection');
+ return;
+ }
+
+ ws.send('Hello World!');
+ showMessage('Sent "Hello World!"');
+ };
+})();
diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html b/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html
new file mode 100644
index 0000000000..c07aa2e87a
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Express session demo</title>
+ </head>
+ <body>
+ <h1>Choose an action.</h1>
+ <button id="login" type="button" title="Simulate login">
+ Simulate login
+ </button>
+ <button id="logout" type="button" title="Simulate logout">
+ Simulate logout
+ </button>
+ <button id="wsButton" type="button" title="Open WebSocket connection">
+ Open WebSocket connection
+ </button>
+ <button id="wsSendButton" type="button" title="Send WebSocket message">
+ Send WebSocket message
+ </button>
+ <pre id="messages" style="height: 400px; overflow: scroll"></pre>
+ <script src="app.js"></script>
+ </body>
+</html>
diff --git a/testing/xpcshell/node-ws/examples/server-stats/index.js b/testing/xpcshell/node-ws/examples/server-stats/index.js
new file mode 100644
index 0000000000..e8754b5b28
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/server-stats/index.js
@@ -0,0 +1,33 @@
+'use strict';
+
+const express = require('express');
+const path = require('path');
+const { createServer } = require('http');
+
+const { WebSocketServer } = require('../..');
+
+const app = express();
+app.use(express.static(path.join(__dirname, '/public')));
+
+const server = createServer(app);
+const wss = new WebSocketServer({ server });
+
+wss.on('connection', function (ws) {
+ const id = setInterval(function () {
+ ws.send(JSON.stringify(process.memoryUsage()), function () {
+ //
+ // Ignore errors.
+ //
+ });
+ }, 100);
+ console.log('started client interval');
+
+ ws.on('close', function () {
+ console.log('stopping client interval');
+ clearInterval(id);
+ });
+});
+
+server.listen(8080, function () {
+ console.log('Listening on http://localhost:8080');
+});
diff --git a/testing/xpcshell/node-ws/examples/server-stats/package.json b/testing/xpcshell/node-ws/examples/server-stats/package.json
new file mode 100644
index 0000000000..20e2029133
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/server-stats/package.json
@@ -0,0 +1,9 @@
+{
+ "author": "",
+ "name": "serverstats",
+ "version": "0.0.0",
+ "repository": "websockets/ws",
+ "dependencies": {
+ "express": "^4.16.4"
+ }
+}
diff --git a/testing/xpcshell/node-ws/examples/server-stats/public/index.html b/testing/xpcshell/node-ws/examples/server-stats/public/index.html
new file mode 100644
index 0000000000..a82815af6f
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/server-stats/public/index.html
@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Server stats</title>
+ <style>
+ table, td {
+ border: 1px solid #333;
+ }
+
+ thead {
+ background-color: #333;
+ color: #fff;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Server stats</h1>
+ <table>
+ <thead>
+ <tr>
+ <th colspan="2">Memory usage</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>RSS</td>
+ <td id="rss"></td>
+ </tr>
+ <tr>
+ <td>Heap total</td>
+ <td id="heapTotal"></td>
+ </tr>
+ <tr>
+ <td>Heap used</td>
+ <td id="heapUsed"></td>
+ </tr>
+ <tr>
+ <td>External</td>
+ <td id="external"></td>
+ </tr>
+ </tbody>
+ </table>
+ <script>
+ (function() {
+ const rss = document.getElementById('rss');
+ const heapTotal = document.getElementById('heapTotal');
+ const heapUsed = document.getElementById('heapUsed');
+ const external = document.getElementById('external');
+ const ws = new WebSocket(`ws://${location.host}`);
+
+ ws.onmessage = function(event) {
+ const data = JSON.parse(event.data);
+
+ rss.textContent = data.rss;
+ heapTotal.textContent = data.heapTotal;
+ heapUsed.textContent = data.heapUsed;
+ external.textContent = data.external;
+ };
+ })();
+ </script>
+ </body>
+</html>
diff --git a/testing/xpcshell/node-ws/examples/ssl.js b/testing/xpcshell/node-ws/examples/ssl.js
new file mode 100644
index 0000000000..a5e750b799
--- /dev/null
+++ b/testing/xpcshell/node-ws/examples/ssl.js
@@ -0,0 +1,37 @@
+'use strict';
+
+const https = require('https');
+const fs = require('fs');
+
+const { WebSocket, WebSocketServer } = require('..');
+
+const server = https.createServer({
+ cert: fs.readFileSync('../test/fixtures/certificate.pem'),
+ key: fs.readFileSync('../test/fixtures/key.pem')
+});
+
+const wss = new WebSocketServer({ server });
+
+wss.on('connection', function connection(ws) {
+ ws.on('message', function message(msg) {
+ console.log(msg.toString());
+ });
+});
+
+server.listen(function listening() {
+ //
+ // If the `rejectUnauthorized` option is not `false`, the server certificate
+ // is verified against a list of well-known CAs. An 'error' event is emitted
+ // if verification fails.
+ //
+ // The certificate used in this example is self-signed so `rejectUnauthorized`
+ // is set to `false`.
+ //
+ const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
+ rejectUnauthorized: false
+ });
+
+ ws.on('open', function open() {
+ ws.send('All glory to WebSockets!');
+ });
+});