Skip to content

Commit ba70685

Browse files
mertcanaltinMert Can Altin
andauthored
add webSocket example (#2626)
* 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 <[email protected]>
1 parent 519b9e1 commit ba70685

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

examples/proxy/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,3 @@ async function run () {
4545
}
4646

4747
run()
48-
49-
// TODO: Add websocket example.

examples/proxy/websocket.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const { Pool, Client } = require('../../')
2+
const http = require('http')
3+
const proxy = require('./proxy')
4+
const WebSocket = require('ws')
5+
6+
const pool = new Pool('http://localhost:4001', {
7+
connections: 256,
8+
pipelining: 1
9+
})
10+
11+
function createWebSocketServer () {
12+
const wss = new WebSocket.Server({ noServer: true })
13+
14+
wss.on('connection', ws => {
15+
ws.on('message', message => {
16+
console.log(`Received message: ${message}`)
17+
ws.send('Received your message!')
18+
})
19+
})
20+
21+
return wss
22+
}
23+
24+
async function run () {
25+
await Promise.all([
26+
new Promise(resolve => {
27+
// Proxy
28+
http.createServer((req, res) => {
29+
proxy({ req, res, proxyName: 'example' }, pool).catch(err => {
30+
if (res.headersSent) {
31+
res.destroy(err)
32+
} else {
33+
for (const name of res.getHeaderNames()) {
34+
res.removeHeader(name)
35+
}
36+
res.statusCode = err.statusCode || 500
37+
res.end()
38+
}
39+
})
40+
}).listen(4000, resolve)
41+
}),
42+
new Promise(resolve => {
43+
// Upstream
44+
http.createServer((req, res) => {
45+
res.end('hello world')
46+
}).listen(4001, resolve)
47+
}),
48+
new Promise(resolve => {
49+
// WebSocket server
50+
const server = http.createServer((req, res) => {
51+
res.writeHead(200, { 'Content-Type': 'text/plain' })
52+
res.end('WebSocket server is running!')
53+
})
54+
55+
const wss = createWebSocketServer()
56+
57+
server.on('upgrade', (request, socket, head) => {
58+
wss.handleUpgrade(request, socket, head, ws => {
59+
wss.emit('connection', ws, request)
60+
})
61+
})
62+
63+
server.listen(4002, resolve)
64+
})
65+
])
66+
67+
const client = new Client('http://localhost:4000')
68+
const { body } = await client.request({
69+
method: 'GET',
70+
path: '/'
71+
})
72+
73+
for await (const chunk of body) {
74+
console.log(String(chunk))
75+
}
76+
77+
// WebSocket client
78+
const ws = new WebSocket('ws://localhost:4002')
79+
ws.on('open', () => {
80+
ws.send('Hello, WebSocket Server!')
81+
})
82+
83+
ws.on('message', message => {
84+
console.log(`WebSocket Server says: ${message}`)
85+
ws.close()
86+
})
87+
}
88+
89+
run()

0 commit comments

Comments
 (0)