當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Upgrader.CheckOrigin方法代碼示例

本文整理匯總了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
}
開發者ID:wlan0,項目名稱:host-api,代碼行數:30,代碼來源:conn.go

示例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
}
開發者ID:zaolab,項目名稱:sunnified,代碼行數:15,代碼來源:context.go

示例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,
	}
}
開發者ID:DmitryDorofeev,項目名稱:gws,代碼行數:16,代碼來源:server.go

示例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)
}
開發者ID:cronosun,項目名稱:buranv1,代碼行數:19,代碼來源:websocket.go

示例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
}
開發者ID:Term1nal,項目名稱:wsboilerplate,代碼行數:24,代碼來源:websocket.go


注:本文中的github.com/gorilla/websocket.Upgrader.CheckOrigin方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。