我正在尝试构建一个包含两部分的服务:后端和前端。两者都在不同的 docker 容器上,并通过 docker-compose 配置和 nginx 容器进行通信。
对于https访问,一切都很好,但是当我尝试使用websocket时,我有一个升级错误,即使Nginx配置得到了这个信息
错误信息 :websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header
我正在使用fasthttp
andfasthttp/websocket
作为我的 Golang 后端。代码在 localhost 上运行(没有 nginx 配置),但是 Docker + nginx 的组合似乎破坏了一些东西。前端使用react
并且是一个简单的let socket = new WebSocket(
wss.mydomain.com/ws/uploadPicture/);
编辑 :
ctx.Request.Header.ConnectionUpgrade()
之前使用时upgrader.Upgrade
,结果是true
,但是ctx.Response.Header.ConnectionUpgrade()
是假的
谢谢 !
Golang 后端
var upgrader = websocket.FastHTTPUpgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {
return true
},
}
func InitRouter() func(*fasthttp.RequestCtx) {
router := fasthttprouter.New()
router.GET("/ws/uploadPicture/", doWS)
return router.Handler
}
func doWS(ctx *fasthttp.RequestCtx) {
err := upgrader.Upgrade(ctx, func(conn *websocket.Conn) {
//SHOULD DO STUFF
})
if (err != nil) {
logs.Error(err.Error()) //HIT THIS ERROR
return
}
}
...
fasthttp.ListenAndServe(`:8000`, InitRouter())
大话西游666
一只斗牛犬
Qyouu
相关分类