zoukankan      html  css  js  c++  java
  • 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 = '
    ';
          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('')
        }
      })

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

  • 相关阅读:
    磁共振中的T1, T2 和 T2*的原理和区别
    Revolver Maps-3D地球仪网站定制
    4种常见网络
    De Moivre–Laplace theorem
    The Complex Inversion Formula. Bromwich contour.
    理解全概率公式与贝叶斯公式(转)
    滴滴数据
    ccs 分类
    dialog problem overview
    recommendation baselines
  • 原文地址:https://www.cnblogs.com/wofeiwofei/p/6054379.html
Copyright © 2011-2022 走看看