utils.go 380 B

1234567891011121314151617181920
  1. package websocket
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. ws "github.com/gorilla/websocket"
  6. )
  7. var upgrader = ws.Upgrader{
  8. CheckOrigin: func(r *http.Request) bool {
  9. return true
  10. },
  11. }
  12. func Conn(ctx *gin.Context) (*ws.Conn, error) {
  13. upgrader.Subprotocols = []string{ctx.GetHeader("Sec-WebSocket-Protocol")}
  14. return upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
  15. }