gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.8k stars 1.61k forks source link

I encountered this error when using websocket, and I want to know how to solve it. #3556

Closed yitiaoyu928 closed 5 months ago

yitiaoyu928 commented 7 months ago

What do you want to ask?

2024-04-30 22:59:18.910 [ERRO] http: response.WriteHeader on hijacked connection from github.com/gogf/gf/v2/net/ghttp/internal/response.(*Writer).WriteHeader (response_writer.go:36) I encountered this problem when I was using goframe2.7.0 with gorilla/websocket to establish a websocket connection, and I wanted to know how to solve it, because I couldn't find a solution. So I was wondering if I could ask a question. Here is my code. import ( "context" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gcmd" "github.com/gorilla/websocket" "net/http"

"demo-socket/internal/controller/hello"

)

var ( update = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r http.Request) bool { return true }, } ) var ( Main = gcmd.Command{ Name: "main", Usage: "main", Brief: "start http server", Func: func(ctx context.Context, parser gcmd.Parser) (err error) { s := g.Server() s.Group("/", func(group ghttp.RouterGroup) { group.ALL("/ws", func(http ghttp.Request) { upgrade, err := update.Upgrade(http.Response.ResponseWriter, http.Request, nil) if err != nil { return } fmt.Println(upgrade)

            })
            group.Bind(
                hello.NewV1(),
            )
        })
        s.Run()
        return nil
    },
}

)

Sorry, my English is not very good. I translated it with translation software.

yitiaoyu928 commented 7 months ago
package cmd

import (
    "context"
    "fmt"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
    "github.com/gogf/gf/v2/os/gcmd"
    "github.com/gorilla/websocket"
    "net/http"

    "demo-socket/internal/controller/hello"
)

var (
    update = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
        CheckOrigin: func(r *http.Request) bool {
            return true
        },
    }
)
var (
    Main = gcmd.Command{
        Name:  "main",
        Usage: "main",
        Brief: "start http server",
        Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
            s := g.Server()
            s.Group("/", func(group *ghttp.RouterGroup) {
                group.ALL("/ws", func(http *ghttp.Request) {
                    upgrade, err := update.Upgrade(http.Response.ResponseWriter, http.Request, nil)
                    if err != nil {
                        return
                    }
                    fmt.Println(upgrade)

                })
                group.Bind(
                    hello.NewV1(),
                )
            })
            s.Run()
            return nil
        },
    }
)
phuonganhniie commented 7 months ago

Hi @yitiaoyu928, I think the error you are encountering is due to an attempt to write headers on an already "hijacked" connection. This issue happens because WebSocket connections effectively take over the underlying HTTP connection, preventing further HTTP operations. That's means writing headers after the connection has been upgraded isn't allowed.

In the GoFrame, request.Response provides an abstraction over the standard http.ResponseWriter, adding extended functionality to control responses more efficiently. If you use request.Response.ResponseWriter directly, it's possible that internal GoFrame abstractions could interfere with the hijacked state, leading to the error. In GoFrame, request.Response.Writer provides direct access to the standard http.ResponseWriter, ensuring compatibility with third-party WebSocket libraries like gorilla/websocket.

This is my full code solution base on your code, hope it might be help you.

package cmd

import (
    "context"
    "fmt"
    "net/http"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
    "github.com/gogf/gf/v2/os/gcmd"
    "github.com/gorilla/websocket"

    "demo-socket/internal/controller/hello"
)

var (
    upgrader = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
        CheckOrigin: func(r *http.Request) bool {
            return true 
        },
    }
)

var (
    Main = gcmd.Command{
        Name:  "main",
        Usage: "main",
        Brief: "start http server",
        Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
            s := g.Server()

            s.Group("/", func(group *ghttp.RouterGroup) {
                group.ALL("/ws", func(request *ghttp.Request) {
                    wsConn, err := upgrader.Upgrade(request.Response.Writer, request.Request, nil)
                    if err != nil {
                        request.Response.WriteHeader(http.StatusInternalServerError)
                        return
                    }
                    defer wsConn.Close() // Ensure to close the connection when server is shutdown or crashed

                    // Here, implement your WebSocket communication logic.
                    // As an example, I'll just print a message and then close the connection.
                    if err := wsConn.WriteMessage(websocket.TextMessage, []byte("Welcome to the WebSocket server!")); err != nil {
                        fmt.Println("Write error:", err)
                    }
                })

                group.Bind(
                    hello.NewV1(),
                )
            })

            s.Run()
            return nil
        },
    }
)

Here is the the connection I tested by Postman:

Screenshot 2024-05-05 at 12 26 59