本文整理匯總了Golang中websocket.Conn.Request方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Request方法的具體用法?Golang Conn.Request怎麽用?Golang Conn.Request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類websocket.Conn
的用法示例。
在下文中一共展示了Conn.Request方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handleWebSocket
func handleWebSocket(conn *websocket.Conn) {
respStatus := http.StatusOK
defer func() {
conn.Close()
logRequest(HTTPS_WEBSOCKET, respStatus, conn.Request().Host, conn.Request())
}()
request := make([]byte, 1024)
for {
n, err := conn.Read(request)
if err != nil {
if debugMode {
log.Error("Error reading on WebSocket: %s", err)
}
break
}
response, status := getLiveItems(string(request[:n]))
if status != http.StatusOK {
respStatus = status
break
}
if len(response) != 0 {
if _, err = conn.Write(response); err != nil {
break
}
}
}
}
示例2: socketHandler
func socketHandler(c *websocket.Conn) {
in, out := make(chan *Message), make(chan *Message)
errc := make(chan error, 1)
// Decode messages from client and send to the in channel.
go func() {
dec := json.NewDecoder(c)
for {
var m Message
if err := dec.Decode(&m); err != nil {
errc <- err
return
}
in <- &m
}
}()
// Receive messages from the out channel and encode to the client.
go func() {
enc := json.NewEncoder(c)
for m := range out {
if err := enc.Encode(m); err != nil {
errc <- err
return
}
}
}()
// open a connection to minimega and handle the request
megaconns := make(map[string]*megaconn)
for {
select {
case m := <-in:
log.Debugln("running snippet from:", c.Request().RemoteAddr)
lOut := limiter(in, out)
megaconns[m.Id] = runMega(m.Id, m.Body, lOut)
case err := <-errc:
if err != io.EOF {
// A encode or decode has failed; bail.
log.Errorln(err)
}
return
}
}
}