-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
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
},
}
) |
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, 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
},
}
) |
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"
)
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)
)
Sorry, my English is not very good. I translated it with translation software.
The text was updated successfully, but these errors were encountered: