[go] solve the cross domain problem of request origin not allowed by upgrader.checkorigin websocket

Time:2021-7-29

If websocket is used under the gin framework, this error will be reported if it is a cross domain request

request origin not allowed by Upgrader.CheckOrigin

 

The websocket library used is  ” github.com/gorilla/websocket”

The following codes need to be added:

upgrader = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
        //Solve cross domain problems
        CheckOrigin: func(r *http.Request) bool {
            return true
        },
    }