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. linux驱动中printk的使用注意事项

    今天在按键驱动中增加printk(KERN_INFO "gpio_keys_gpio_isr()\n");在驱动加载阶段可以输出调试信息,但驱动加载起来后的信息,在串口端看不到输出 ...

  2. web前端之HTML的前世今生

    一个尖括号   < 一个尖括号能干什么    < ? 你可以编出一顶帽子  <(:-p 或一张笑脸    :-> 或诉说一份爱   <3 或者更直接一些 <!DOC ...

  3. 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)

    物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)   2015年11月14日|    by: nbboy|    Category: 系统设计, 缓存设计, 高性能系统 摘要 ...

  4. Bootstrap使用初涉

    在这里记录一下搭建Bootstrap的开发环境: 首先手头上的有Bootstrap的相关资料,这里用的是bootstrap-3.3.5-dist. 在开发一个Web项目的时候要将述的资料都导入到项目中 ...

  5. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. Fair Scheduler 队列设置经验总结

    Fair Scheduler 队列设置经验总结 由于公司的hadoop集群的计算资源不是很充足,需要开启yarn资源队列的资源抢占.在使用过程中,才明白资源抢占的一些特点.在这里总结一下. 只有一个队 ...

  7. thinkphp3.2自定义配置文件

    扩展配置可以支持自动加载额外的自定义配置文件,并且配置格式和项目配置一样. 设置扩展配置的方式如下(多个文件用逗号分隔): // 加载扩展配置文件 'LOAD_EXT_CONFIG' => 'u ...

  8. servlet学习笔记_3

    一.路径问题如果是在浏览器端请求服务器的数据(超链接,js的src),那么加/代表在Tomcat的webapp目录,不加/的话通常不考虑,实际上不加/在浏览器端也是当前项目目录(但是开发中通常必须要写 ...

  9. JSON API免费接口

    来自:http://www.bejson.com/knownjson/webInterface/ 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuId ...

  10. pyhon——进程线程、与协程基础概述

    一直以来写博客都是实用主义者,只写用法,没信心写原理,但是每一次写作业的过程都有一种掘地三尺的感觉,终于,写博客困难症重症患者经历了漫长的思想斗争,还是决定把从网上淘到的各种杂货和自己的总结放在一起, ...