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


Golang websocket.IsUnexpectedCloseError函數代碼示例

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


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

示例1: readPump

// readPump pumps messages from the websocket connection to the hub.
func (c *Conn) readPump() {
	defer func() {
		hub.unregister <- c
		c.ws.Close()
	}()
	c.ws.SetReadLimit(maxMessageSize)
	c.ws.SetReadDeadline(time.Now().Add(pongWait))
	c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, message, err := c.ws.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v", err)
			}
			break
		}
		message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))

		msg := &Message{}
		err = json.Unmarshal(message, msg)
		bo := strings.Split(string(message), ",")
		target := hub.connections[bo[0]]
		if target != nil {
			target.send <- []byte(bo[1])
		}

		if err != nil {
			// byte message
			continue
		}
		//service.SendMessage(msg, string(message))
		//		hub.broadcast <- message
	}
}
開發者ID:cs6402,項目名稱:goose,代碼行數:35,代碼來源:conn.go

示例2: writePump

// writePump pumps messages from the toplevel to the websocket connection.
func (c *Client) writePump() {
	defer func() {
		c.conn.Close()
		c.top.terminate()
	}()

	buf := make([]byte, 4*1024)
	for {
		n, err := c.top.stdout.Read(buf)
		//		log.Printf("Read %s", buf[:n])
		if n > 0 {
			err := c.conn.WriteMessage(websocket.TextMessage, buf[:n])
			if err != nil {
				if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway,
					websocket.CloseNoStatusReceived) {
					log.Printf("Writepump websocket error: %s", err)
				}

				break
			}
		} else {
			if err != nil {
				if err != io.EOF {
					log.Printf("Writepump Unexpected error while reading STDOUT from process: %s", err)
				} else {
					log.Printf("Writepump Process STDOUT closed: %s", err)
				}
				break
			}
		}
	}
}
開發者ID:kfl,項目名稱:trymosml,代碼行數:33,代碼來源:client.go

示例3: handleOutput

func (h *Hijacker) handleOutput(conn *websocket.Conn, pio ProcessIO) int {
	var exitStatus int
	for {
		var output atc.HijackOutput
		err := conn.ReadJSON(&output)
		if err != nil {
			if !websocket.IsCloseError(err) && !websocket.IsUnexpectedCloseError(err) {
				fmt.Println(err)
			}
			break
		}

		if output.ExitStatus != nil {
			exitStatus = *output.ExitStatus
		} else if len(output.Error) > 0 {
			fmt.Fprintf(os.Stderr, "%s\n", ansi.Color(output.Error, "red+b"))
			exitStatus = 255
		} else if len(output.Stdout) > 0 {
			pio.Out.Write(output.Stdout)
		} else if len(output.Stderr) > 0 {
			pio.Err.Write(output.Stderr)
		}
	}

	return exitStatus
}
開發者ID:ArthurHlt,項目名稱:fly,代碼行數:26,代碼來源:hijacker.go

示例4: readPump

// readPump pumps messages from the websocket connection to the hub.
func (c *connection) readPump() {
	defer func() {
		h.unregister <- c
		c.ws.Close()
	}()

	c.ws.SetReadLimit(maxMessageSize)
	c.ws.SetReadDeadline(time.Now().Add(pongWait))
	c.ws.SetPongHandler(func(string) error {
		c.ws.SetReadDeadline(time.Now().Add(pongWait))
		return nil
	})

	for {
		_, message, err := c.ws.ReadMessage()
		fmt.Println(string(message))

		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v", err)
			}
			break
		}
	}
}
開發者ID:pselibas,項目名稱:semaphore,代碼行數:26,代碼來源:handler.go

示例5: handleConnectionError

func handleConnectionError(connection *Connection, err error) {
	if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
		log.Printf("Unexpected error from connection: %q", err)
	}

	hub.Unregister(connection)
}
開發者ID:frankdejonge,項目名稱:golang-websocket-example,代碼行數:7,代碼來源:connection.go

示例6: connectionHandler

//Callback function that is called for new connection.
func connectionHandler(w http.ResponseWriter, r *http.Request, server *WServer) {
	conn, err := gorillaUpgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println("connectionHandler: gorillaUpgrader: ", err)
		return
	}
	defer func() {
		//dev
		log.Println("Connection close: ", conn.RemoteAddr())
		conn.Close()
	}()

	//dev
	log.Println("New connection: ", conn.RemoteAddr())

	client := Client{server.OnMessage, conn}
	defer server.OnClose(&client)
	for {
		messageType, r, err := conn.NextReader()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Println("Connection handler: ", err)
			}
			return
		}
		new_msg := &Message{&r, messageType}
		client.onmessage(new_msg, &client)
	}
}
開發者ID:angrypie,項目名稱:remote-desktop,代碼行數:30,代碼來源:wserver.go

示例7: readPump

// readPump pumps messages from the websocket connection to the hub.
func (c *Client) readPump() {
	defer func() {
		c.hub.unregister <- c
		c.conn.Close()
	}()
	c.conn.SetReadLimit(maxMessageSize)
	c.conn.SetReadDeadline(time.Now().Add(pongWait))
	c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, message, err := c.conn.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v", err)
			} else {
				log.Printf("Client disconnected")
			}
			break
		}
		if c.hub.read {
			c.hub.ReadBuffer.Lock()
			c.hub.ReadBuffer.Messages = append(c.hub.ReadBuffer.Messages, &message)
			c.hub.ReadBuffer.Unlock()
		}
	}
}
開發者ID:emccode,項目名稱:mars-challenge,代碼行數:26,代碼來源:websocket.go

示例8: readPump

// readPump pumps messages from the websocket connection to the hub.
func (u *User) readPump() {
	defer func() {
		u.g.unregister <- u
		u.ws.Close()
	}()

	u.ws.SetReadLimit(maxMessageSize)
	u.ws.SetReadDeadline(time.Now().Add(pongWait))
	u.ws.SetPongHandler(func(string) error {
		u.ws.SetReadDeadline(time.Now().Add(pongWait))
		return nil
	})

	for {
		_, message, err := u.ws.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				// TODO: do something??
			}

			u.Log.Error(err.Error())

			return
		}

		u.Log.WithField("content", string(message)).Debug("new message")

		c := NewCommand(u, string(message))

		u.g.commands <- c
	}
}
開發者ID:wyattjoh,項目名稱:spacegophers,代碼行數:33,代碼來源:user.go

示例9: 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))
	}
}
開發者ID:funkygao,項目名稱:gafka,代碼行數:26,代碼來源:handler_sub_ws.go

示例10: ExampleIsUnexpectedCloseError

// The websocket.IsUnexpectedCloseError function is useful for identifying
// application and protocol errors.
//
// This server application works with a client application running in the
// browser. The client application does not explicitly close the websocket. The
// only expected close message from the client has the code
// websocket.CloseGoingAway. All other other close messages are likely the
// result of an application or protocol error and are logged to aid debugging.
func ExampleIsUnexpectedCloseError(err error, c *websocket.Conn, req *http.Request) {
	for {
		messageType, p, err := c.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v, user-agent: %v", err, req.Header.Get("User-Agent"))
			}
			return
		}
		processMesage(messageType, p)
	}
}
開發者ID:CNDonny,項目名稱:scope,代碼行數:20,代碼來源:example_test.go

示例11: readPump

// readPump pumps messages from the websocket connection to the hub.
func (s *subscription) readPump() {
	defer func() {
		Hub.unregister <- s
		s.conn.ws.Close()
	}()
	s.conn.ws.SetReadLimit(maxMessageSize)
	s.conn.ws.SetReadDeadline(time.Now().Add(pongWait))
	s.conn.ws.SetPongHandler(func(string) error {
		log.Println("[DEBUG] In pong handler", s.conn.username)
		s.conn.ws.SetReadDeadline(time.Now().Add(pongWait))
		return nil
	})
	for {
		_, message, err := s.conn.ws.ReadMessage()
		if err != nil {

			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v", err)
			}
			log.Printf("error: %v", err)
			break
		}
		// Parse as JSON.
		var m Message
		err = json.Unmarshal(message, &m)
		if err != nil {
			log.Println("error unmarshaling: ", err)
			break
		}
		// Save raw data for this message for further processing, but append
		// username.
		// XXX: we'll end up unmarshalling twice. We should re-think this later.
		m.realm = s.realm
		m.From = s.conn.username
		// Remarshal to m.rawdata
		rawdata, err := json.Marshal(m)
		if err != nil {
			log.Println("Error re-marshalling: ", err)
			break
		}
		m.rawdata = rawdata
		// Pass it on to the external handler.
		Hub.handler.HandleMessage(m)
		// For right now only PrivateMT should not be broadcast.
		if m.Mtype != PrivateMT {
			Hub.broadcast <- m
		}

	}
}
開發者ID:domino14,項目名稱:gosports,代碼行數:51,代碼來源:conn.go

示例12: readPump

// readPump pumps messages from the websocket connection to the toplevel.
func (c *Client) readPump() {
	defer func() {
		c.reg.unregister <- c
		c.conn.Close()
		c.top.terminate()
	}()

	for {
		_, message, err := c.conn.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway,
				websocket.CloseNoStatusReceived) {
				log.Printf("readPump error: %v", err)
			}
			break
		}
		c.top.stdin.Write(message)
	}
}
開發者ID:kfl,項目名稱:trymosml,代碼行數:20,代碼來源:client.go

示例13: readPump

// readPump pumps messages from the websocket connection to the hub.
func (c *Conn) readPump() {
	defer func() {
		hub.unregister <- c
		c.ws.Close()
	}()
	c.ws.SetReadLimit(maxMessageSize)
	c.ws.SetReadDeadline(time.Now().Add(pongWait))
	c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, message, err := c.ws.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
				log.Printf("error: %v", err)
			}
			break
		}
		message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
		hub.broadcast <- message
	}
}
開發者ID:Fruchtgummi,項目名稱:websocket,代碼行數:21,代碼來源:conn.go

示例14: read

func (c *Client) read() {
	hub := GetHub()
	defer func() {
		hub.unregister <- c
		c.con.Close()
	}()
	c.con.SetReadLimit(maxMessageSize)
	c.con.SetReadDeadline(time.Now().Add(pongWait))
	c.con.SetPongHandler(func(string) error { c.con.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		message := &SendMessage{}
		err := c.con.ReadJSON(message)
		if err != nil {
			if soc.IsUnexpectedCloseError(err, soc.CloseGoingAway) {
				log.Printf("error: %v", err)
			}
			break
		}
		message.Message = string(bytes.TrimSpace(bytes.Replace([]byte(message.Message), newline, space, -1)))
		hub.broadcast <- message
	}
}
開發者ID:Crandel,項目名稱:golang_websocket_chat_demo,代碼行數:22,代碼來源:client.go

示例15: readMessagesLoop

// readMessagesLoop reads messages from the websocket connection.
func (c *wsConnection) readMessagesLoop(mode int) {
	// When disconnect
	defer func() {
		close(c.done)
		close(c.outboxMessages)
		close(c.inboxMessages)
	}()

	for {
		// Reset the read deadline.
		c.ws.SetReadDeadline(time.Now().Add(c.sc.pingTimeout))

		messageType, message, err := c.ws.ReadMessage()

		if err != nil {
			isUnexpectedError := websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure,
				websocket.CloseGoingAway, websocket.CloseNoStatusReceived)

			if err != io.EOF && isUnexpectedError {
				log.Error("failed to read data from websocket: %v", err)
			}

			return
		}

		switch messageType {
		case mode:
			log.Debug("received message with a payload")
			c.inboxMessages <- message
		case websocket.CloseMessage:
			log.Debug("got close message")
			return
		default:
			log.Debug("got wrong message, message dropped")
		}
	}
}
開發者ID:ZloyDyadka,項目名稱:socketcrutch,代碼行數:38,代碼來源:ws_connection.go


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