本文整理匯總了Golang中github.com/gorilla/websocket.Upgrader.CheckOrigin方法的典型用法代碼示例。如果您正苦於以下問題:Golang Upgrader.CheckOrigin方法的具體用法?Golang Upgrader.CheckOrigin怎麽用?Golang Upgrader.CheckOrigin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/websocket.Upgrader
的用法示例。
在下文中一共展示了Upgrader.CheckOrigin方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetConnection
func GetConnection(rw http.ResponseWriter, req *http.Request) (*connection, error) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
upgrader.CheckOrigin = func(r *http.Request) bool {
// allow all connections by default
return true
}
conn := &connection{}
for _, v := range req.Header["Connection"] {
for _, i := range strings.Split(v, ",") {
if strings.EqualFold(strings.TrimSpace(i), "upgrade") {
webConn, err := upgrader.Upgrade(rw, req, nil)
if err != nil {
return nil, err
}
conn.webConn = webConn
return conn, nil
}
}
}
conn.rw = rw
return conn, nil
}
示例2: ToWebSocket
func (this *Context) ToWebSocket(upgrader *websocket.Upgrader, header http.Header) (err error) {
if upgrader == nil {
upgrader = &websocket.Upgrader{}
upgrader.CheckOrigin = func(_ *http.Request) bool {
return true
}
upgrader.Error = func(w http.ResponseWriter, r *http.Request, status int, err error) {
log.Println(err)
http.Error(w, http.StatusText(status), status)
}
}
this.WebSocket, err = upgrader.Upgrade(this.RootResponse(), this.Request, header)
return
}
示例3: newWsHandler
func newWsHandler(c Config, r Responder) *wsHandler {
u := websocket.Upgrader{}
if c.Origin != "" {
originChecker := glob.MustCompile(c.Origin)
u.CheckOrigin = func(r *http.Request) bool {
return originChecker.Match(r.Header.Get(headerOrigin))
}
}
return &wsHandler{
upgrader: u,
config: c,
responder: r,
}
}
示例4: serveWsJson
func (self *websocketStruct) serveWsJson(ctx *httpserver.RequestContext) {
if ctx.Request.Method != "GET" {
http.Error(ctx.Writer, "Method not allowed", 405)
return
}
u := new(websocket.Upgrader)
u.CheckOrigin = func(r *http.Request) bool {
return true
}
ws, err := u.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {
log.Println(err)
return
}
self.json.onNewConnection(ws)
}
示例5: NewServer
// NewServer returns an instance of a WsServer configuration.
func NewServer(maxbytes int64, bufsize int, checkorigin bool) (error, *Server) {
if maxbytes < 1 {
return fmt.Errorf("Server Max Bytes cannot be less than 1, value given: %v", maxbytes), nil
}
u := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
if !checkorigin {
u.CheckOrigin = checkOriginStub
}
s := &Server{
maxBytes: maxbytes,
wsBufSize: bufsize,
NewConn: make(chan *Connection, 10),
upgrader: u,
}
return nil, s
}