Example #1
0
// read from the web socket, copying to the pty master
// messages are expected to be text and base64 encoded
func sendConnectionInputToPty(conn *websocket.Conn, reader io.ReadCloser, f *os.File, finalizer *ReadWriteRoutingFinalizer) {
	defer closeReader(reader, f, finalizer)

	for {
		mt, payload, err := conn.ReadMessage()
		if err != nil {
			if !isNormalWsError(err) {
				log.Printf("conn.ReadMessage failed: %s\n", err)
			}
			return
		}
		switch mt {
		case websocket.BinaryMessage:
			log.Printf("Ignoring binary message: %q\n", payload)
		case websocket.TextMessage:
			var msg WebSocketMessage
			if err := json.Unmarshal(payload, &msg); err != nil {
				log.Printf("Invalid message %s\n", err)
				continue
			}
			if errMsg := handleMessage(msg, f); errMsg != nil {
				log.Printf(errMsg.Error())
				return
			}
		default:
			log.Printf("Invalid websocket message type %d\n", mt)
			return
		}
	}
}
Example #2
0
// Reads the message from the websocket connection until error is received,
// returns the channel which should be used for reading such messages.
func readMessages(conn *websocket.Conn) chan *WsMessage {
	messagesChan := make(chan *WsMessage)
	go func() {
		for {
			_, bytes, err := conn.ReadMessage()
			messagesChan <- &WsMessage{err: err, bytes: bytes}
			if err != nil {
				close(messagesChan)
				break
			}
		}
	}()
	return messagesChan
}