golang go语言 反向 websocket 代理演示代码
通过go语言实现websocket反向代理功能

package main

import (
	"fmt"
	"github.com/fasthttp/websocket"
	"github.com/valyala/fasthttp"
	proxy "github.com/yeqown/fasthttp-reverse-proxy"
	"log"
)

var upgraders = &websocket.FastHTTPUpgrader{
ReadBufferSize:  1024,
WriteBufferSize: 1024,
// 解决跨域问题
CheckOrigin: func(r *fasthttp.RequestCtx) bool {
return true
},
}
//配置代理地址和路径
var (
	proxyServer = proxy.NewWSReverseProxy("192.168.0.62:9960", "/server/PUPPET_SERVER/toId-Server/123123")
)

func ProxyHandler(ctx *fasthttp.RequestCtx) {
	switch string(ctx.Path()) {
	case "/":
		proxyServer = proxy.NewWSReverseProxy("192.168.0.62:9960",string(ctx.Path()))

		proxyServer.ServeHTTP(ctx)
	default:
		proxyServer = proxy.NewWSReverseProxy("192.168.0.62:9960",string(ctx.Path()))
		/*ctx.Error("Unsupported path", fasthttp.StatusNotFound)*/
		proxyServer.ServeHTTP(ctx)
	}
}

func main() {
	proxy.DefaultUpgrader=upgraders
	// 8081为监听端口
	if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
		log.Fatal(err)
	}

	fmt.Println("hello")
}

go读取配置文件参考url地址:https://studygolang.com/subject/2

例如:访问地址:ws://127.0.0.1:8081/server/PUPPET_SERVER/toId-Server/456
代理并跳转地址:ws://192.168.0.62:9960/server/PUPPET_SERVER/toId-Server/456

代理是双向的,可以互相发送和接收信息

注:gateway网关也可以做代理功能,但是网关代理限制发送消息大小为64k,所以用go语言编写的代理是最好的选择。