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

How to support WebSocket #3206

Open
peterwillcn opened this issue Jun 20, 2022 · 1 comment
Open

How to support WebSocket #3206

peterwillcn opened this issue Jun 20, 2022 · 1 comment

Comments

@peterwillcn
Copy link

peterwillcn commented Jun 20, 2022

How to gin + gorilla/websocket support WebSocket

@yeqown
Copy link

yeqown commented Jun 24, 2022

gorilla/websocket has provided sample codes like this:

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
}

func handler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Println(err)
        return
    }
    // ... Use conn to send and receive messages.
}

So, in my opinion, you just wonder how to get http.Request and http.ResponseWriter from gin.Context? That's simple:

func handler(c *gin.Context) {
    conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
    if err != nil {
        log.Println(err)
        return
    }
    // ... Use conn to send and receive messages.
}

now all others are same to official document.

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

No branches or pull requests

2 participants