github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/internal/websocket/handler.go (about)

     1  package websocket
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gin-gonic/gin"
     7  	"github.com/gorilla/websocket"
     8  	"github.com/martinohmann/rfoutlet/internal/command"
     9  )
    10  
    11  // Handler accepts websocket connections and creates a clients to handle them.
    12  func Handler(hub *Hub, queue chan<- command.Command) gin.HandlerFunc {
    13  	upgrader := websocket.Upgrader{
    14  		ReadBufferSize:  1024,
    15  		WriteBufferSize: 1024,
    16  		CheckOrigin: func(r *http.Request) bool {
    17  			return true
    18  		},
    19  	}
    20  
    21  	return func(c *gin.Context) {
    22  		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
    23  		if err != nil {
    24  			log.Errorf("failed to upgrade websocket connection: %v", err)
    25  			return
    26  		}
    27  
    28  		newClient(hub, conn, queue).listen()
    29  	}
    30  }