From febd70e8d4cb0acc381821fb3a843a62193b1ffc Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 18 Jan 2024 16:42:43 +0300 Subject: [PATCH 1/5] add webSocket example --- examples/proxy/index.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/examples/proxy/index.js b/examples/proxy/index.js index 5f35049203e..dbe9d1eb369 100644 --- a/examples/proxy/index.js +++ b/examples/proxy/index.js @@ -1,6 +1,7 @@ const { Pool, Client } = require('../../') const http = require('http') const proxy = require('./proxy') +const WebSocket = require('ws') const pool = new Pool('http://localhost:4001', { connections: 256, @@ -30,6 +31,31 @@ async function run () { http.createServer((req, res) => { res.end('hello world') }).listen(4001, resolve) + }), + new Promise(resolve => { + // WebSocket server + const wss = new WebSocket.Server({ noServer: true }) + wss.on('connection', ws => { + ws.on('message', message => { + console.log(`Received message: ${message}`) + ws.send('Received your message!') + }) + }) + + // Create an HTTP server to upgrade the connection to WebSocket + const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }) + res.end('WebSocket server is running!') + }) + + // Upgrade HTTP server to support WebSocket + server.on('upgrade', (request, socket, head) => { + wss.handleUpgrade(request, socket, head, ws => { + wss.emit('connection', ws, request) + }) + }) + + server.listen(4002, resolve) }) ]) @@ -42,8 +68,17 @@ async function run () { for await (const chunk of body) { console.log(String(chunk)) } + + // WebSocket client + const ws = new WebSocket('ws://localhost:4002') + ws.on('open', () => { + ws.send('Hello, WebSocket Server!'); + }); + + ws.on('message', message => { + console.log(`WebSocket Server says: ${message}`) + ws.close(); + }); } run() - -// TODO: Add websocket example. From c6895f43f60f0a40f2054a884ccf21b8f4453ae4 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 18 Jan 2024 16:46:11 +0300 Subject: [PATCH 2/5] lint --- examples/proxy/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/proxy/index.js b/examples/proxy/index.js index dbe9d1eb369..902e4ddcf59 100644 --- a/examples/proxy/index.js +++ b/examples/proxy/index.js @@ -72,13 +72,13 @@ async function run () { // WebSocket client const ws = new WebSocket('ws://localhost:4002') ws.on('open', () => { - ws.send('Hello, WebSocket Server!'); - }); + ws.send('Hello, WebSocket Server!') + }) ws.on('message', message => { console.log(`WebSocket Server says: ${message}`) - ws.close(); - }); + ws.close() + }) } run() From 1a8407201487a45720df03b3147a6860795375b7 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 25 Jan 2024 00:27:39 +0300 Subject: [PATCH 3/5] refactor: Organize proxy-related code into separate files --- examples/proxy/index.js | 14 +++----------- examples/proxy/websocket.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 examples/proxy/websocket.js diff --git a/examples/proxy/index.js b/examples/proxy/index.js index 902e4ddcf59..6758acd1d69 100644 --- a/examples/proxy/index.js +++ b/examples/proxy/index.js @@ -1,7 +1,7 @@ const { Pool, Client } = require('../../') const http = require('http') const proxy = require('./proxy') -const WebSocket = require('ws') +const createWebSocketServer = require('./websocket') const pool = new Pool('http://localhost:4001', { connections: 256, @@ -34,21 +34,13 @@ async function run () { }), new Promise(resolve => { // WebSocket server - const wss = new WebSocket.Server({ noServer: true }) - wss.on('connection', ws => { - ws.on('message', message => { - console.log(`Received message: ${message}`) - ws.send('Received your message!') - }) - }) - - // Create an HTTP server to upgrade the connection to WebSocket const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('WebSocket server is running!') }) - // Upgrade HTTP server to support WebSocket + const wss = createWebSocketServer() + server.on('upgrade', (request, socket, head) => { wss.handleUpgrade(request, socket, head, ws => { wss.emit('connection', ws, request) diff --git a/examples/proxy/websocket.js b/examples/proxy/websocket.js new file mode 100644 index 00000000000..5d937ea8a69 --- /dev/null +++ b/examples/proxy/websocket.js @@ -0,0 +1,16 @@ +const WebSocket = require('ws'); + +function createWebSocketServer() { + const wss = new WebSocket.Server({ noServer: true }); + + wss.on('connection', ws => { + ws.on('message', message => { + console.log(`Received message: ${message}`); + ws.send('Received your message!'); + }); + }); + + return wss; +} + +module.exports = createWebSocketServer; \ No newline at end of file From df4f53a7066efa596b6adae94ce0a8cf48ce5eef Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 25 Jan 2024 00:27:52 +0300 Subject: [PATCH 4/5] refactor: Organize proxy-related code into separate files --- examples/proxy/index.js | 1 + examples/proxy/websocket.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/proxy/index.js b/examples/proxy/index.js index 6758acd1d69..8e7097146c8 100644 --- a/examples/proxy/index.js +++ b/examples/proxy/index.js @@ -2,6 +2,7 @@ const { Pool, Client } = require('../../') const http = require('http') const proxy = require('./proxy') const createWebSocketServer = require('./websocket') +const WebSocket = require('ws') const pool = new Pool('http://localhost:4001', { connections: 256, diff --git a/examples/proxy/websocket.js b/examples/proxy/websocket.js index 5d937ea8a69..3cc9d6d4d5d 100644 --- a/examples/proxy/websocket.js +++ b/examples/proxy/websocket.js @@ -1,16 +1,16 @@ -const WebSocket = require('ws'); +const WebSocket = require('ws') -function createWebSocketServer() { - const wss = new WebSocket.Server({ noServer: true }); +function createWebSocketServer () { + const wss = new WebSocket.Server({ noServer: true }) wss.on('connection', ws => { ws.on('message', message => { - console.log(`Received message: ${message}`); - ws.send('Received your message!'); - }); - }); + console.log(`Received message: ${message}`) + ws.send('Received your message!') + }) + }) - return wss; + return wss } -module.exports = createWebSocketServer; \ No newline at end of file +module.exports = createWebSocketServer From 11299b7a3e240cb75ac950e2efe957caf2b7fc8c Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 25 Jan 2024 12:06:29 +0300 Subject: [PATCH 5/5] feat: move websocket instance to separate file --- examples/proxy/index.js | 30 --------------- examples/proxy/websocket.js | 75 ++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/examples/proxy/index.js b/examples/proxy/index.js index 8e7097146c8..4cf8bc61b16 100644 --- a/examples/proxy/index.js +++ b/examples/proxy/index.js @@ -1,8 +1,6 @@ const { Pool, Client } = require('../../') const http = require('http') const proxy = require('./proxy') -const createWebSocketServer = require('./websocket') -const WebSocket = require('ws') const pool = new Pool('http://localhost:4001', { connections: 256, @@ -32,23 +30,6 @@ async function run () { http.createServer((req, res) => { res.end('hello world') }).listen(4001, resolve) - }), - new Promise(resolve => { - // WebSocket server - const server = http.createServer((req, res) => { - res.writeHead(200, { 'Content-Type': 'text/plain' }) - res.end('WebSocket server is running!') - }) - - const wss = createWebSocketServer() - - server.on('upgrade', (request, socket, head) => { - wss.handleUpgrade(request, socket, head, ws => { - wss.emit('connection', ws, request) - }) - }) - - server.listen(4002, resolve) }) ]) @@ -61,17 +42,6 @@ async function run () { for await (const chunk of body) { console.log(String(chunk)) } - - // WebSocket client - const ws = new WebSocket('ws://localhost:4002') - ws.on('open', () => { - ws.send('Hello, WebSocket Server!') - }) - - ws.on('message', message => { - console.log(`WebSocket Server says: ${message}`) - ws.close() - }) } run() diff --git a/examples/proxy/websocket.js b/examples/proxy/websocket.js index 3cc9d6d4d5d..324d0531c57 100644 --- a/examples/proxy/websocket.js +++ b/examples/proxy/websocket.js @@ -1,5 +1,13 @@ +const { Pool, Client } = require('../../') +const http = require('http') +const proxy = require('./proxy') const WebSocket = require('ws') +const pool = new Pool('http://localhost:4001', { + connections: 256, + pipelining: 1 +}) + function createWebSocketServer () { const wss = new WebSocket.Server({ noServer: true }) @@ -13,4 +21,69 @@ function createWebSocketServer () { return wss } -module.exports = createWebSocketServer +async function run () { + await Promise.all([ + new Promise(resolve => { + // Proxy + http.createServer((req, res) => { + proxy({ req, res, proxyName: 'example' }, pool).catch(err => { + if (res.headersSent) { + res.destroy(err) + } else { + for (const name of res.getHeaderNames()) { + res.removeHeader(name) + } + res.statusCode = err.statusCode || 500 + res.end() + } + }) + }).listen(4000, resolve) + }), + new Promise(resolve => { + // Upstream + http.createServer((req, res) => { + res.end('hello world') + }).listen(4001, resolve) + }), + new Promise(resolve => { + // WebSocket server + const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }) + res.end('WebSocket server is running!') + }) + + const wss = createWebSocketServer() + + server.on('upgrade', (request, socket, head) => { + wss.handleUpgrade(request, socket, head, ws => { + wss.emit('connection', ws, request) + }) + }) + + server.listen(4002, resolve) + }) + ]) + + const client = new Client('http://localhost:4000') + const { body } = await client.request({ + method: 'GET', + path: '/' + }) + + for await (const chunk of body) { + console.log(String(chunk)) + } + + // WebSocket client + const ws = new WebSocket('ws://localhost:4002') + ws.on('open', () => { + ws.send('Hello, WebSocket Server!') + }) + + ws.on('message', message => { + console.log(`WebSocket Server says: ${message}`) + ws.close() + }) +} + +run()