简体   繁体   English

节点代理Web套接字如何检查

[英]Node proxy web sockets how to check

I use the following module and it works fine for reverse proxy https://github.com/nodejitsu/node-http-proxy currently I've used the code like the following example 我使用以下模块,并且它对于反向代理https://github.com/nodejitsu/node-http-proxy正常工作,我已经使用了类似以下示例的代码

httpProxy.createServer({
  target: 'ws://localhost:9014',
  ws: true
}).listen(8014);

my question is how can I check/simulate that the websockets are working? 我的问题是如何检查/模拟 websocket是否正常工作? Any test will be helpful... 任何测试都会有所帮助...

In response to the OP's request for browser test, I modified my original solution to proxy both HTTP and WS traffic to a server where an index.html file is served. 为了响应OP对浏览器测试的要求,我修改了原始解决方案,以将HTTP和WS流量代理到提供index.html文件的服务器。 This file then connects the browser to the proxy server via WebSocket, which the proxy then proxies to the main server. 然后,该文件通过WebSocket将浏览器连接到代理服务器,然后代理将代理连接到主服务器。 A simple message is printed on the browser document from the main server. 主服务器在浏览器文档上打印一条简单消息。

So that there is no need to copy/paste anything, I created this repo with full instruction: https://github.com/caasjj/httpproxy.git 为了无需复制/粘贴任何内容,我使用完整的指令创建了此仓库: https : //github.com/caasjj/httpproxy.git

Here is the code in case others want to look at it here. 如果其他人想在这里查看,这里是代码。 To run the whole thing, create the two server files and the index.html file, start the servers with node proxyreceiver.js and node proxyserver.js and then navigate to localhost:8014/index.html . 要运行整个过程,请创建两个服务器文件和index.html文件,使用node proxyreceiver.jsnode proxyserver.js启动服务器,然后导航到localhost:8014/index.html

( proxyserver.js ): proxyserver.js ):

var httpProxy = require('http-proxy');
var http = require('http');

var proxy = new httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 9014
  }
});
var proxyServer = http.createServer(function (req, res) {
  proxy.web(req, res);
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

proxyServer.listen(8014);

( proxyreceiver.js ): proxyreceiver.js ):

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(9014);

function handler (req, res) {
    res.writeHead(200);
    fs.readFile('index.html', function(err, data){
      res.end(data);
    })

}

io.on('connection', function (socket) {

  socket.emit('data', { message: 'Hello World!' });

  socket.on('resp', function(msg) {
    console.log('Got message: ', msg);
  });

});

( index.html ): index.html ):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Web Socket Proxy Test</title>
  <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
  <script>
    var socket = io('http://localhost:8014');
    var p = document.createElement("p")
    socket.on('data', function (data) {
       console.log('Got', data);
       p.innerHTML = "Received:" + data.message;
       document.body.appendChild(p);
    });
</script>
</head>
<body>
  <h1>Test ProxyServer</h1>
</body>
</html>

The best way to test is to create a client to connect to it. 最好的测试方法是创建一个客户端以与其连接。 there are many ws modules around. 周围有许多ws模块。 Or you can use this: https://www.websocket.org/echo.html just put your url there and test it. 或者,您可以使用以下方法: https : //www.websocket.org/echo.html只需将您的网址放在此处并进行测试即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM