-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add webSocket example * lint * refactor: Organize proxy-related code into separate files * refactor: Organize proxy-related code into separate files * feat: move websocket instance to separate file --------- Co-authored-by: Mert Can Altin <mert.altin@trendyol.com>
- Loading branch information
1 parent
519b9e1
commit ba70685
Showing
2 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,3 @@ async function run () { | |
} | ||
|
||
run() | ||
|
||
// TODO: Add websocket example. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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 }) | ||
|
||
wss.on('connection', ws => { | ||
ws.on('message', message => { | ||
console.log(`Received message: ${message}`) | ||
ws.send('Received your message!') | ||
}) | ||
}) | ||
|
||
return wss | ||
} | ||
|
||
async function run () { | ||
await Promise.all([ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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() |
Maybe Promise.allSettled can be better in this case.