本文整理匯總了Golang中github.com/gorilla/websocket.Conn.SetPongHandler方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.SetPongHandler方法的具體用法?Golang Conn.SetPongHandler怎麽用?Golang Conn.SetPongHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/websocket.Conn
的用法示例。
在下文中一共展示了Conn.SetPongHandler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: New
// New takes a websocket and creates a ShellClient object implementing the
// engines.Shell interface.
func New(ws *websocket.Conn) *ShellClient {
stdinReader, stdin := ioext.BlockedPipe()
tellOut := make(chan int, 10)
tellErr := make(chan int, 10)
stdout, stdoutWriter := ioext.AsyncPipe(shellconsts.ShellMaxPendingBytes, tellOut)
stderr, stderrWriter := ioext.AsyncPipe(shellconsts.ShellMaxPendingBytes, tellErr)
stdinReader.Unblock(shellconsts.ShellMaxPendingBytes)
s := &ShellClient{
ws: ws,
stdin: stdin,
stdout: stdout,
stderr: stderr,
stdinReader: stdinReader,
stdoutWriter: stdoutWriter,
stderrWriter: stderrWriter,
done: make(chan struct{}),
}
ws.SetReadLimit(shellconsts.ShellMaxMessageSize)
ws.SetReadDeadline(time.Now().Add(shellconsts.ShellPongTimeout))
ws.SetPongHandler(s.pongHandler)
go s.writeMessages()
go s.readMessages()
go s.sendPings()
go s.sendAck(shellconsts.StreamStdout, tellOut)
go s.sendAck(shellconsts.StreamStderr, tellErr)
return s
}
示例2: wsReadPump
func (this *subServer) wsReadPump(clientGone chan struct{}, ws *websocket.Conn) {
ws.SetReadLimit(this.wsReadLimit)
ws.SetReadDeadline(time.Now().Add(this.wsPongWait))
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(this.wsPongWait))
return nil
})
// if kateway shutdown while there are open ws conns, the shutdown will
// wait 1m: this.subServer.wsPongWait
for {
_, message, err := ws.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Warn("%s: %v", ws.RemoteAddr(), err)
} else {
log.Debug("%s: %v", ws.RemoteAddr(), err)
}
close(clientGone)
break
}
log.Debug("ws[%s] read: %s", ws.RemoteAddr(), string(message))
}
}
示例3: New
// New returns a new display client implementing the ioext.ReadWriteCloser
// interface using a websocket.
//
// The DisplayClient essentially takes care of sending and receiving ping/pongs
// to keep the websocket alive. However, the DisplayClient does read/write
// directly on websocket without any buffering, hence, you must keep calling
// Read() with a non-empty buffer to keep the connection alive.
func New(ws *websocket.Conn) *DisplayClient {
c := &DisplayClient{
ws: ws,
}
ws.SetReadLimit(displayconsts.DisplayMaxMessageSize)
ws.SetReadDeadline(time.Now().Add(displayconsts.DisplayPongTimeout))
ws.SetPongHandler(c.pongHandler)
go c.sendPings()
return c
}
示例4: 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
}
}
}
示例5: reader
func (ws *WebSocker) reader(conn *websocket.Conn) {
defer conn.Close()
conn.SetReadLimit(512)
conn.SetReadDeadline(time.Now().Add(pongWait))
conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, _, err := conn.ReadMessage()
if err != nil {
break
}
}
}
示例6: imwork
func imwork(wsConn *websocket.Conn) {
wsConn.SetReadLimit(maxMessageSize)
wsConn.SetReadDeadline(time.Now().Add(pongWait))
wsConn.SetPongHandler(func(string) error { wsConn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, msgBytes, err := wsConn.ReadMessage()
if err != nil {
fmt.Println(err)
break
}
// fmt.Println("receive:", msgBytes)
go procMsg(msgBytes)
}
}
示例7: 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
}
}
}
示例8: NewWs
func NewWs(ws *websocket.Conn, bufSize int, pingPeriod time.Duration) *Ws {
ws.SetPongHandler(func(msg string) error {
sent, err := strconv.ParseInt(msg, 36, 64)
if err != nil {
log.Warningln("Wrong pong time:", msg)
return nil
}
log.Infof("Ping time: %dns\n", time.Now().UnixNano()-sent)
return nil
})
return &Ws{
Conn: ws,
pingPeriod: pingPeriod,
copyBuf: make([]byte, bufSize),
}
}
示例9: pumpStdin
// pumpStdin handles reading data from the websocket connection and writing
// it to stdin of the process.
func pumpStdin(conn *websocket.Conn, stdin io.WriteCloser) {
// Setup our connection's websocket ping/pong handlers from our const values.
defer conn.Close()
conn.SetReadLimit(maxMessageSize)
conn.SetReadDeadline(time.Now().Add(pongWait))
conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := conn.ReadMessage()
if err != nil {
break
}
message = append(message, '\n')
if _, err := stdin.Write(message); err != nil {
break
}
}
}
示例10: wsReader
func wsReader(ws *websocket.Conn, mc chan string) {
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 {
_, msg, err := ws.ReadMessage()
if err != nil {
log.Println("Error when reading websocket message")
log.Println(err)
break
}
log.Println("Recieved message: " + string(msg))
mc <- string(msg)
}
}
示例11: messagesHandler
func messagesHandler(ws *websocket.Conn, ticker *time.Ticker, msg Event, c chan Event, e chan error) {
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(PONG_WAIT))
return nil
})
for {
err := ws.ReadJSON(&msg)
if err != nil {
log.Println("READ ERR")
ticker.Stop()
e <- err
}
if reflect.TypeOf(msg).String() == "tutum.Event" {
c <- msg
}
}
}
示例12: messagesHandler
func messagesHandler(ws *websocket.Conn, ticker *time.Ticker, msg Event, c chan Event, e chan error, e2 chan error) {
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(PONG_WAIT))
return nil
})
for {
err := ws.ReadJSON(&msg)
if err != nil {
e <- err
e2 <- err
time.Sleep(4 * time.Second)
} else {
if reflect.TypeOf(msg).String() == "tutum.Event" {
c <- msg
}
}
}
}
示例13: reader
// reader is the guts of this package. It takes the stdin and stdout pipes
// of the cmd we created in ServeWS and pipes them between the client and server
// over websockets.
func reader(conn *websocket.Conn, stdout io.ReadCloser, stdin io.WriteCloser) {
// Setup our connection's websocket ping/pong handlers from our const values.
conn.SetReadLimit(maxMessageSize)
conn.SetReadDeadline(time.Now().Add(pongWait))
conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
tickerChan := make(chan bool)
defer close(tickerChan) // make sure to close the ticker when we are done.
go ticker(conn, tickerChan)
for {
msgType, r, err := conn.NextReader()
if err != nil {
if msgType == -1 {
return // we got a disconnect from the client. We are good to close.
}
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""), time.Time{})
return
}
w, err := conn.NextWriter(msgType)
if err != nil {
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""), time.Time{})
return
}
if _, err := io.Copy(stdin, r); err != nil {
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""), time.Time{})
return
}
go func() {
if _, err := io.Copy(w, stdout); err != nil {
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""), time.Time{})
return
}
if err := w.Close(); err != nil {
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseGoingAway, ""), time.Time{})
return
}
}()
}
}
示例14: responseAndAnswer
// readPump pumps messages from the websocket connection to the hub.
func responseAndAnswer(ws *websocket.Conn, client string, r *http.Request, w http.ResponseWriter) {
defer func() {
ws.Close()
}()
ws.SetReadLimit(maxMessageSize)
ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
//enemyFromCookie:=""
for {
_, message, err := ws.ReadMessage()
if err != nil {
break
}
//enemyNameCookie, _ := r.Cookie("enemyName")
//if (enemyNameCookie!=nil){
// enemyFromCookie =enemyNameCookie.Value
//}
//fmt.Println(string(message))
names := string(message[:bytes.Index(message, []byte(":"))])
colons := bytes.Index(message, []byte(":"))
identifier := string(message[colons+1 : colons+9])
//fmt.Println("IDDDDDDDDDDD "+identifier)
switch identifier {
case "clientTo":
askInvitation(names)
break
case "Accepted":
acceptInvitation(names, colons, client, w)
break
case "CheckVal":
getSideValue(client)
break
case "MadeStep":
madeStep(client, names)
case "QuitGame":
quitGame(w, r, names)
}
}
}
示例15: reader
/* reader reads input from the web socket connection and sends it to the master.
* reader runs in its own goroutine.
* If the connection closes, the reader goroutine terminates.
*/
func reader(conn *websocket.Conn, ch chan readerResult) {
conn.SetPongHandler(func(appData string) error {
ch <- readerResult{conn, 10, nil, nil}
return nil
})
conn.SetPingHandler(func(appData string) error {
ch <- readerResult{conn, 9, nil, nil}
return nil
})
for {
messageType, data, err := conn.ReadMessage()
readerResult := readerResult{conn, messageType, data, err}
ch <- readerResult
if err != nil {
return
}
}
}