[GO]解决request origin not allowed by Upgrader.CheckOrigin websocket跨域

2020-10-28 11:39:11 浏览数 (1)

在gin框架下使用websocket , 如果是跨域请求的时候会报这个错误

request origin not allowed by Upgrader.CheckOrigin

使用的websocket库是 "github.com/gorilla/websocket"

需要增加以下代码:

代码语言:javascript复制
    upgrader = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
        // 解决跨域问题
        CheckOrigin: func(r *http.Request) bool {
            return true
        },
    }

0 人点赞