相关疑难解决方法(0)

使用节点http-proxy代理websocket连接

我有一个通过socket.io使用websockets的应用程序.对于我的应用程序,我想使用单独的HTTP服务器为我的应用程序提供静态内容和JavaScript.因此,我需要设置一个代理.

我正在使用node-http-proxy.作为起点,我在端口8081上运行我的websockets应用程序.我使用以下代码将socket.io通信重定向到此独立服务器,同时使用express来提供静态内容:

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

// create a server
var app = express();
var proxy = httpProxy.createProxyServer({ ws: true });

// proxy HTTP GET / POST
app.get('/socket.io/*', function(req, res) {
  console.log("proxying GET request", req.url);
  proxy.web(req, res, { target: 'http://localhost:8081'});
});
app.post('/socket.io/*', function(req, res) {
  console.log("proxying POST request", req.url);
  proxy.web(req, res, { target: 'http://localhost:8081'});
});

// Proxy websockets
app.on('upgrade', function (req, socket, head) {
  console.log("proxying upgrade request", req.url);
  proxy.ws(req, socket, head);
}); …
Run Code Online (Sandbox Code Playgroud)

websocket node.js socket.io node-http-proxy

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

node-http-proxy ×1

node.js ×1

socket.io ×1

websocket ×1