package ws import ( "bufio" "fmt" "io" "os" "github.com/gorilla/websocket" ) // SendStdinLines reads lines from the Stdin buffer, sends them in the // websocket and reads a reply. func SendStdinLines(con *websocket.Conn) error { var ( err error l, msg []byte inp = bufio.NewReader(os.Stdin) ) for err != io.EOF { fmt.Print("> ") if l, err = inp.ReadBytes('\n'); err != nil && err != io.EOF { return fmt.Errorf("unable to read from stdin: %v", err) } if len(l) != 0 { l = l[:len(l)-1] } if err = con.WriteMessage(websocket.TextMessage, l); err != nil { return fmt.Errorf("unable to write on websocket on '%s': %v", con.RemoteAddr(), err) } if _, msg, err = con.ReadMessage(); err != nil { return fmt.Errorf("unable to read from websocket on '%s': %v", con.RemoteAddr(), err) } fmt.Printf("(%s): %s\n", con.RemoteAddr(), msg) } return nil }