const http = require('http')
const server = http.createServer((req, res) =>{
res.end('hello world');
}).listen(8080)
server.on('upgrade', (req, client, head) => {
const headers = _getProxyHeader(req.headers) //将客户端的websocket头和一些信息转发到真正的处理服务器上
headers.hostname = 'localhost'//目标服务器
headers.path = '/' 目标路径
headers.port = 6443
const proxy = http.request(headers) //https可用https,headers中加入rejectUnauthorized=false忽略证书验证
proxy.on('upgrade', (res, socket, head) => {
client.write(_formatProxyResponse(res))//使用目标服务器头信息响应客户端
client.pipe(socket)
socket.pipe(client)
})
proxy.on('error', (error) => {
client.write("Sorry, cant't connect to this container ")
return
})
proxy.end()
function _getProxyHeader(headers) {
const keys = Object.getOwnPropertyNames(headers)
const proxyHeader = { headers: {} }
keys.forEach(key => {
if (key.indexOf('sec') >= 0 || key === 'upgrade' || key === 'connection') {
proxyHeader.headers[key] = headers[key]
return
}
proxyHeader[key] = headers[key]
})
return proxyHeader
}
function _formatProxyResponse(res) {
const headers = res.headers
const keys = Object.getOwnPropertyNames(headers)
let switchLine = '\r\n';
let response = [`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}${switchLine}`]
keys.forEach(key => {
response.push(`${key}: ${headers[key]}${switchLine}`)
})
response.push(switchLine)
return response.join('')
}
})

暂时没有问题,如遇到欢迎指出。

nodejs 转发websocket (websocket proxy)的更多相关文章

  1. NodeJS反向代理websocket

    如需转载请标明出处:http://blog.csdn.net/itas109QQ技术交流群:129518033 文章目录NodeJS反向代理websocket@[toc]前言代码相关问题:1.http ...

  2. 使用nginx作为websocket的proxy server

    blog.csdn.net/zhx6044/article/details/50278765 WebSocket WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选 ...

  3. nodejs——js 实现webSocket 兼容移动端

    nodejs——js 实现webSocket 兼容移动端 //服务器端 //npm install --save ws const express = require('express'); cons ...

  4. Spring Cloud Gateway转发Spring WebSocket

    Spring Cloud Gateway转发Spring WebSocket 源码:https://github.com/naah69/SpringCloud-Gateway-WebSocket-De ...

  5. NodeJs 实现简单WebSocket 即时通讯

    至于服务器语言选择nodeJs,一是因为自己是做前端的,对javascript比较熟悉,相比于其他后台语言,自然会更喜欢nodeJs了, 二是NodeJs本身事件驱动的方式很擅长与大量客户端保持高并发 ...

  6. 用nodejs快速实现websocket服务端(带SSL证书生成)

    有不少公司将nodejs的socket.io作为websocket的解决方案,很遗憾的是socket.io是对websocket的封装,并不支持html5原始的websocket协议,微信小程序使用的 ...

  7. NodeJS client code websocket

    var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...

  8. 如何实现从 Redis 中订阅消息转发到 WebSocket 客户端

    PHP 的redis扩展是阻塞式 IO ,使用订阅/发布模式时,会导致整个进程进入阻塞.因此必须使用Swoole\Redis异步客户端来实现. 实例代码 $server = new swoole_we ...

  9. Websocket——Websocket原理

    偶然在知乎上看到一篇回帖,瞬间认为之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比較喜欢看这样的博客,读起来非常轻松.不枯燥,没有布道师的 ...

随机推荐

  1. 广度优先搜索(BFS)

    定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...

  2. 如何让老Mac机支持USB安装Windows

    一些老Mac机的用户想装Windows,却发现自己的系统上的Boot Camp Assistant(以下简称BCA)没有USB安装Windows的选项. 下面以我的MacBook Pro (13-in ...

  3. Selenium碰到的异常记录

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...

  4. 在tomcat中配置jdk的不同版本

    在tomcat中配置jdk的不同版本---------------------------------------------------------------------------------- ...

  5. 慕课网__CSS__line height

  6. bootstrap笔记

    一.栅格系统:<div class="container">内容</div>固定宽度,1200px-margin==1170px<div class= ...

  7. Asp.Net_Mvc_@Html.xxx()的扩展

    /// <summary> /// 生成分类下拉-列表框,选中指定的项 /// </summary> /// <param name="html"&g ...

  8. spring框架学习(二)依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.接口注入不作要求,下面介绍前两种方式. 1,set注入 采用属性的set方法进行初始化,就成为set注入. 1)给普 ...

  9. JAVA开发错误总结(仅记录遇到的错误---后续不断更新......)

    =======华丽分割线(工具总结)===================== 1:Maven项目中junit测试找不到主类的问题 Class not found com.test.utils.tes ...

  10. ie8兼容性(不支持trim 、readonly光标、乱码encodeURI())

    IE8下String的Trim()方法失效的解决方案 1.用jquery的trim()方法,$.trim(str)就可以了. 2.String扩展: 第一种 String.prototype.trim ...