nodejs 转发websocket (websocket proxy)
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)的更多相关文章
- NodeJS反向代理websocket
如需转载请标明出处:http://blog.csdn.net/itas109QQ技术交流群:129518033 文章目录NodeJS反向代理websocket@[toc]前言代码相关问题:1.http ...
- 使用nginx作为websocket的proxy server
blog.csdn.net/zhx6044/article/details/50278765 WebSocket WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选 ...
- nodejs——js 实现webSocket 兼容移动端
nodejs——js 实现webSocket 兼容移动端 //服务器端 //npm install --save ws const express = require('express'); cons ...
- Spring Cloud Gateway转发Spring WebSocket
Spring Cloud Gateway转发Spring WebSocket 源码:https://github.com/naah69/SpringCloud-Gateway-WebSocket-De ...
- NodeJs 实现简单WebSocket 即时通讯
至于服务器语言选择nodeJs,一是因为自己是做前端的,对javascript比较熟悉,相比于其他后台语言,自然会更喜欢nodeJs了, 二是NodeJs本身事件驱动的方式很擅长与大量客户端保持高并发 ...
- 用nodejs快速实现websocket服务端(带SSL证书生成)
有不少公司将nodejs的socket.io作为websocket的解决方案,很遗憾的是socket.io是对websocket的封装,并不支持html5原始的websocket协议,微信小程序使用的 ...
- NodeJS client code websocket
var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...
- 如何实现从 Redis 中订阅消息转发到 WebSocket 客户端
PHP 的redis扩展是阻塞式 IO ,使用订阅/发布模式时,会导致整个进程进入阻塞.因此必须使用Swoole\Redis异步客户端来实现. 实例代码 $server = new swoole_we ...
- Websocket——Websocket原理
偶然在知乎上看到一篇回帖,瞬间认为之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比較喜欢看这样的博客,读起来非常轻松.不枯燥,没有布道师的 ...
随机推荐
- Cheatsheet: 2016 12.01 ~ 12.31
Other Code review in remote teams 5 Signs That Your REST API Isn't RESTful Web Server Side React Sta ...
- xmemcached的使用
转载 http://www.voidcn.com/blog/u010408365/article/p-4972896.html xmemcached主要特性 高性能 XMemcached同样是基于ja ...
- NE Upgrade python script. Need to write a Tkinter GUI for it
# -*- coding: utf-8 -*-#from ftplib import FTP __authour__='CC' import osimport telnetlibimport time ...
- 代码性能优化——task
var t1 = Task.Factory.StartNew(delegate { //代码(查接口.数据库) }); 缺点: 不能使用request( HttpContext.Current.Req ...
- 【原】命令行方式开启WIFI热点
由于Wifi比较慢,可以使用笔记本(带无线网卡的电脑也可以) 开启无线网络,供手机使用.方法如下: netsh wlan set hostednetwork mode=allow ssid=qa ...
- Meta标签用法大全
meta是html文档在head标签里定义的一个对文档进行描述的功能性标签 meta标签有下面的作用: 1.搜索引擎优化(SEO) 2.定义页面使用语言 3.自动刷新并指向新的页面 4.实现网页转换时 ...
- 特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?
特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的? 陈雨桐 1. 全球范围: 根据 CFA 协会 2014 年 6 月的报告: CFA Institute has over ...
- typeof,GetType
typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...
- Deep Learning 24:读论文“Batch-normalized Maxout Network in Network”——mnist错误率为0.24%
读本篇论文“Batch-normalized Maxout Network in Network”的原因在它的mnist错误率为0.24%,世界排名第4.并且代码是用matlab写的,本人还没装caf ...
- 转:图解Git[强烈推荐]
https://my.oschina.net/xdev/blog/114383 再次感谢原著作者和中文翻译者. 此页图解git中的最常用命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透 ...