我想在HTTP_PROXY
的{{1}}容器上使用docker
模拟websocket。常规的docker-compose
和HTTP
请求可以正常工作并得到预期的代理,但是代理中没有调用websockets的升级事件。
我的HTTPS
容器中的相关代码,用于测试网络套接字
app
为代理Websocket网络流量,我在容器上定义了以下环境变量
const WebSocket = require('ws');
const ws = new WebSocket('ws://echo.websocket.org/', {
origin: 'http://websocket.org'
});
ws.on('open', function open() {
console.log('connected');
ws.send(Date.now());
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.on('message', function incoming(data) {
console.log(data)
console.log(`Roundtrip time: ${Date.now() - data} ms`);
setTimeout(function timeout() {
ws.send(Date.now());
}, 500);
});
然后,如果我调用常规的environment:
NODE_TLS_REJECT_UNAUTHORIZED: 0
HTTP_PROXY: http://proxy:8000
HTTPS_PROXY: http://proxy:8001
NO_PROXY: localhost,127.0.0.1
请求
HTTP
在我的axios.get('http://example.org')
容器中,它的代码看起来像这样:
proxy
但是无论我做什么,const http = require('http')
const https = require('https')
const httpProxy = require('http-proxy')
const pem = require('pem')
pem.createCertificate({ days: 1, selfSigned: true }, (certErr, keys) => {
if (certErr) {
throw certErr
}
const httpsOptions = {
key: keys.serviceKey,
cert: keys.certificate,
}
const proxy = httpProxy.createServer({
ssl: httpsOptions,
ws: true,
secure: false,
})
const secureServer = https.createServer(httpsOptions, (req, res) => {
console.log('here here http secure', req.url)
proxy.web(req, res, { target: 'http://mock:1880' })
})
const server = http.createServer((req, res) => {
console.log('here here http non secure', req.url)
proxy.web(req, res, { target: 'http://mock:1880' })
})
server.on('upgrade', (req, socket, head) => {
console.log('here here socket', req, socket, head)
proxy.ws(req, socket, { target: 'ws://mock:1880/signalr' })
})
proxy.listen(8010)
secureServer.listen(8001)
server.listen(8000)
})
事件都不会被调用。
那么我如何使用此代理模拟网络套接字?