Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yitiaoyu928 opened this issue Apr 30, 2024 · 2 comments

Comments

@yitiaoyu928
Copy link

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
Copy link
Author

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants