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


Golang websocket.Conn類代碼示例

本文整理匯總了Golang中galapagia/Godeps/_workspace/src/github.com/gorilla/websocket.Conn的典型用法代碼示例。如果您正苦於以下問題:Golang Conn類的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: ping

func ping(ws *websocket.Conn, done chan struct{}) {
	ticker := time.NewTicker(pingPeriod)
	defer ticker.Stop()
	for {
		select {
		case <-ticker.C:
			if err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait)); err != nil {
				log.Println("ping:", err)
			}
		case <-done:
			return
		}
	}
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:14,代碼來源:main.go

示例2: generateConnWriter

/*
Connections support one concurrent reader and one concurrent writer.
Applications are responsible for ensuring that no more than one goroutine calls
the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON)
concurrently and that no more than one goroutine calls the read methods
(NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler,
SetPingHandler) concurrently.
 -- http://www.gorillatoolkit.org/pkg/websocket
*/
func generateConnWriter(conn *websocket.Conn) chan<- interface{} {
	c := make(chan interface{})
	go func() {
		for {
			j := <-c
			err := conn.WriteJSON(j)
			if err != nil {
				fmt.Println("Error writing JSON:", err)
				return
			}
		}
	}()
	return c
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:23,代碼來源:dataWebSocket.go

示例3: writer

func writer(ws *websocket.Conn, lastMod time.Time) {
	lastError := ""
	pingTicker := time.NewTicker(pingPeriod)
	fileTicker := time.NewTicker(filePeriod)
	defer func() {
		pingTicker.Stop()
		fileTicker.Stop()
		ws.Close()
	}()
	for {
		select {
		case <-fileTicker.C:
			var p []byte
			var err error

			p, lastMod, err = readFileIfModified(lastMod)

			if err != nil {
				if s := err.Error(); s != lastError {
					lastError = s
					p = []byte(lastError)
				}
			} else {
				lastError = ""
			}

			if p != nil {
				ws.SetWriteDeadline(time.Now().Add(writeWait))
				if err := ws.WriteMessage(websocket.TextMessage, p); err != nil {
					return
				}
			}
		case <-pingTicker.C:
			ws.SetWriteDeadline(time.Now().Add(writeWait))
			if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
				return
			}
		}
	}
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:40,代碼來源:main.go

示例4: pumpStdout

func pumpStdout(ws *websocket.Conn, r io.Reader, done chan struct{}) {
	defer func() {
		ws.Close()
		close(done)
	}()
	s := bufio.NewScanner(r)
	for s.Scan() {
		ws.SetWriteDeadline(time.Now().Add(writeWait))
		if err := ws.WriteMessage(websocket.TextMessage, s.Bytes()); err != nil {
			break
		}
	}
	if s.Err() != nil {
		log.Println("scan:", s.Err())
	}
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:16,代碼來源:main.go

示例5: internalError

func internalError(ws *websocket.Conn, msg string, err error) {
	log.Println(msg, err)
	ws.WriteMessage(websocket.TextMessage, []byte("Internal server error."))
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:4,代碼來源:main.go

示例6: pumpStdin

func pumpStdin(ws *websocket.Conn, w io.Writer) {
	defer ws.Close()
	ws.SetReadLimit(maxMessageSize)
	ws.SetReadDeadline(time.Now().Add(pongWait))
	ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, message, err := ws.ReadMessage()
		if err != nil {
			break
		}
		message = append(message, '\n')
		if _, err := w.Write(message); err != nil {
			break
		}
	}
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:16,代碼來源:main.go

示例7: reader

func reader(ws *websocket.Conn) {
	defer ws.Close()
	ws.SetReadLimit(512)
	ws.SetReadDeadline(time.Now().Add(pongWait))
	ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, _, err := ws.ReadMessage()
		if err != nil {
			break
		}
	}
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:12,代碼來源:main.go


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