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


Golang Conn.Request方法代碼示例

本文整理匯總了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
			}
		}
	}
}
開發者ID:elimisteve,項目名稱:togethr,代碼行數:27,代碼來源:live-server.go

示例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
		}
	}
}
開發者ID:npe9,項目名稱:minimega,代碼行數:45,代碼來源:socket.go


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