本文整理匯總了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))
}
示例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)
}
示例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))
}
示例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()
}
示例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())
}