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


Golang Conn.Subprotocol方法代碼示例

本文整理匯總了Golang中github.com/gorilla/websocket.Conn.Subprotocol方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Subprotocol方法的具體用法?Golang Conn.Subprotocol怎麽用?Golang Conn.Subprotocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gorilla/websocket.Conn的用法示例。


在下文中一共展示了Conn.Subprotocol方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleWebsocket

func (s *WebsocketServer) handleWebsocket(conn *websocket.Conn) {
	//out.Debug("New WS connection: %s", conn)
	var serializer Serializer
	var payloadType int
	if proto, ok := s.protocols[conn.Subprotocol()]; ok {
		serializer = proto.serializer
		payloadType = proto.payloadType
	} else {
		// TODO: this will not currently ever be hit because
		//       gorilla/websocket will reject the conncetion
		//       if the subprotocol isn't registered
		switch conn.Subprotocol() {
		case jsonWebsocketProtocol:
			serializer = new(JSONSerializer)
			payloadType = websocket.TextMessage
		case msgpackWebsocketProtocol:
			serializer = new(MessagePackSerializer)
			payloadType = websocket.BinaryMessage
		default:
			conn.Close()
			return
		}
	}

	peer := websocketPeer{
		conn:        conn,
		serializer:  serializer,
		messages:    make(chan Message, 10),
		payloadType: payloadType,
	}
	go peer.run()

	logErr(s.Node.Accept(&peer))
}
開發者ID:exis-io,項目名稱:node,代碼行數:34,代碼來源:websocket_server.go

示例2: handleWebsocket

func (s *WebsocketServer) handleWebsocket(conn *websocket.Conn) {
	var serializer Serializer
	var payloadType int
	if proto, ok := s.protocols[conn.Subprotocol()]; ok {
		serializer = proto.serializer
		payloadType = proto.payloadType
	} else {
		// TODO: this will not currently ever be hit because
		//       gorilla/websocket will reject the conncetion
		//       if the subprotocol isn't registered
		switch conn.Subprotocol() {
		case jsonWebsocketProtocol:
			serializer = new(JSONSerializer)
			payloadType = websocket.TextMessage
		case msgpackWebsocketProtocol:
			serializer = new(MessagePackSerializer)
			payloadType = websocket.BinaryMessage
		default:
			conn.Close()
			return
		}
	}

	peer := websocketPeer{
		conn:        conn,
		serializer:  serializer,
		messages:    make(chan Message, 10),
		payloadType: payloadType,
	}
	go func() {
		for {
			// TODO: use conn.NextMessage() and stream
			// TODO: do something different based on binary/text frames
			if _, b, err := conn.ReadMessage(); err != nil {
				conn.Close()
				break
			} else {
				msg, err := serializer.Deserialize(b)
				if err != nil {
					// TODO: handle error
				} else {
					peer.messages <- msg
				}
			}
		}
	}()
	s.Router.Accept(&peer)
}
開發者ID:beatgammit,項目名稱:turnpike,代碼行數:48,代碼來源:websocket_server.go

示例3: handleWebsocket

func (s *WebsocketServer) handleWebsocket(conn *websocket.Conn) {
	var serializer Serializer
	var payloadType int
	s.lock.RLock()
	if proto, ok := s.protocols[conn.Subprotocol()]; ok {
		s.lock.RUnlock()
		serializer = proto.serializer
		payloadType = proto.payloadType
	} else {
		s.lock.RUnlock()

		// TODO: this will not currently ever be hit because
		//       gorilla/websocket will reject the conncetion
		//       if the subprotocol isn't registered
		switch conn.Subprotocol() {
		case jsonWebsocketProtocol:
			serializer = new(JSONSerializer)
			payloadType = websocket.TextMessage
		case msgpackWebsocketProtocol:
			serializer = new(MessagePackSerializer)
			payloadType = websocket.BinaryMessage
		default:
			conn.Close()
			return
		}
	}

	peer := websocketPeer{
		conn:             conn,
		serializer:       serializer,
		sendMsgs:         make(chan Message, 16),
		messages:         make(chan Message, 100),
		payloadType:      payloadType,
		closing:          make(chan struct{}),
		ConnectionConfig: &s.ConnectionConfig,
	}
	go peer.run()

	logErr(s.Router.Accept(&peer))
}
開發者ID:apcera,項目名稱:sample-apps,代碼行數:40,代碼來源:websocket_server.go

示例4: handler

func (l *listener) handler(ws *websocket.Conn, req *http.Request) {
	l.lock.Lock()

	if !l.running {
		ws.Close()
		l.lock.Unlock()
		return
	}

	if ws.Subprotocol() != l.proto.Name()+".sp.nanomsg.org" {
		ws.Close()
		l.lock.Unlock()
		return
	}

	w := &wsPipe{ws: ws, addr: l.addr, proto: l.proto, open: true}
	w.dtype = websocket.BinaryMessage
	w.iswss = l.iswss
	w.ws.SetReadLimit(int64(l.maxrx))

	w.props = make(map[string]interface{})
	w.props[mangos.PropLocalAddr] = ws.LocalAddr()
	w.props[mangos.PropRemoteAddr] = ws.RemoteAddr()

	if req.TLS != nil {
		w.props[mangos.PropTLSConnState] = *req.TLS
	}

	w.wg.Add(1)
	l.pending = append(l.pending, w)
	l.cv.Broadcast()
	l.lock.Unlock()

	// We must not return before the socket is closed, because
	// our caller will close the websocket on our return.
	w.wg.Wait()
}
開發者ID:kildevaeld,項目名稱:projects,代碼行數:37,代碼來源:ws.go

示例5: getWsConnId

// XXX: pass this as a log context (gorilla) object
func getWsConnId(r *http.Request, ws *websocket.Conn) string {
	return fmt.Sprintf("ws:/%v %v (subprotocol %+v)",
		r.URL.Path, ws.RemoteAddr(), ws.Subprotocol())
}
開發者ID:hpcloud,項目名稱:logyard-apps,代碼行數:5,代碼來源:handler.go


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