func (w *WebsocketServer) streamWebsocket(websocketSink *websocket.WebsocketSink, websocketConnection *gorilla.Conn, register func(sinks.Sink) bool, unregister func(sinks.Sink)) {
	register(websocketSink)
	defer unregister(websocketSink)

	go websocketConnection.ReadMessage()
	server.NewKeepAlive(websocketConnection, w.keepAliveInterval).Run()
}
func makeTestHandler(keepAliveCompleted chan struct{}) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		conn, _ := websocket.Upgrade(rw, req, nil, 0, 0)
		go conn.ReadMessage()
		server.NewKeepAlive(conn, 50*time.Millisecond).Run()
		close(keepAliveCompleted)
	})
}
func (w *WebsocketServer) streamWebsocket(appId string, websocketConnection *gorilla.Conn, register func(sinks.Sink) bool, unregister func(sinks.Sink)) {
	websocketSink := websocket.NewWebsocketSink(
		appId,
		w.logger,
		websocketConnection,
		w.bufferSize,
		w.dropsondeOrigin,
	)

	register(websocketSink)
	defer unregister(websocketSink)

	go websocketConnection.ReadMessage()
	server.NewKeepAlive(websocketConnection, w.keepAliveInterval).Run()
}
Beispiel #4
0
func (w *WebsocketServer) streamLogs(appId string, ws *gorilla.Conn) {
	w.logger.Debugf("WebsocketServer: Requesting a wss sink for app %s", appId)
	websocketSink := websocket.NewWebsocketSink(
		appId,
		w.logger,
		ws,
		w.bufferSize,
	)

	w.sinkManager.RegisterSink(websocketSink)
	defer w.sinkManager.UnregisterSink(websocketSink)

	go ws.ReadMessage()
	server.NewKeepAlive(ws, w.keepAliveInterval).Run()
}
func (h *websocketHandler) runWebsocketUntilClosed(ws *websocket.Conn) (closeCode int, closeMessage string) {
	keepAliveExpired := make(chan struct{})
	clientWentAway := make(chan struct{})

	// TODO: remove this loop (but keep ws.ReadMessage()) once we retire support in the cli for old style keep alives
	go func() {
		for {
			_, _, err := ws.ReadMessage()
			if err != nil {
				close(clientWentAway)
				h.logger.Debugf("websocket handler: connection from %s was closed", ws.RemoteAddr().String())
				return
			}
		}
	}()

	go func() {
		server.NewKeepAlive(ws, h.keepAlive).Run()
		close(keepAliveExpired)
		h.logger.Debugf("websocket handler: Connection from %s timed out", ws.RemoteAddr().String())
	}()

	closeCode = websocket.CloseNormalClosure
	closeMessage = ""
	for {
		select {
		case <-clientWentAway:
			return
		case <-keepAliveExpired:
			closeCode = websocket.ClosePolicyViolation
			closeMessage = "Client did not respond to ping before keep-alive timeout expired."
			return
		case message, ok := <-h.messages:
			if !ok {
				h.logger.Debug("websocket handler: messages channel was closed")
				return
			}
			err := ws.WriteMessage(websocket.BinaryMessage, message)
			if err != nil {
				h.logger.Debugf("websocket handler: Error writing to websocket: %s", err.Error())
				return
			}
		}
	}
}