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('')
        }
      })

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

  • 相关阅读:
    什么是跨域?什么是同源策略?如何解决
    安装路由的环境
    react的开发环境
    遍历列表,遍历对象,以及组件
    redux 状态管理工具
    react的钩子函数
    json-server
    react中的setState,受控组件和非受控组件以及组件的传值
    vue中的插槽
    react遍历列表
  • 原文地址:https://www.cnblogs.com/wofeiwofei/p/6054379.html
Copyright © 2011-2022 走看看