Update client

This commit is contained in:
raul 2024-04-25 10:24:27 +02:00
parent 7248c4d30c
commit 6cb6ff1103
2 changed files with 21 additions and 37 deletions

View File

@ -7,27 +7,26 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
"log"
//"os"
"os"
)
// clientCmd represents the client command
var clientCmd = &cobra.Command{
Use: "client",
Short: "Client interface for tiny-chat",
Short: "Client interface for mini-chat",
Long: `Refactored mini-chat client that properly interfaces
with its server.
Example:
./tiny-chat client --ip 192.168.0.100 --port 1337`,
./mini-chat client --ip 192.168.0.100 --port 1337`,
Run: func(cmd *cobra.Command, args []string) {
// if len(args) == 0 {
// cmd.Help()
// os.Exit(0)
// }
if err := setClientParameters(cmd); err != nil {
log.Fatalf("Error happened trying to set parameters: %v\n", err)
cmd.Help()
fmt.Printf("\n-----------------------\n")
fmt.Println(err)
fmt.Printf("-----------------------\n")
os.Exit(0)
}
Client()

View File

@ -1,17 +1,19 @@
/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
package cmd
import (
"bufio"
"fmt"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
"log"
"net"
"os"
"strings"
"time"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
//"time"
)
type Message struct {
@ -60,9 +62,14 @@ func listenMessages(g *gocui.Gui) {
for {
messageFromServer, err := receiveMessage(data.Server)
if err != nil {
// Avoid triggering an error if client quits the client
if err == gocui.ErrQuit {
g.Close()
} else {
g.Close()
log.Fatalf("Error occurred reading from server: %v\n", err)
}
}
formattedMessage := strings.TrimRight(messageFromServer, "\n")
fmt.Fprintln(chatbox, formattedMessage)
@ -109,25 +116,6 @@ func GUI() {
}
}
// func listener(g *gocui.Gui, conn net.Conn) {
// time.Sleep(time.Second)
// chatbox, err := g.View("chatbox")
// if err != nil {
// log.Panicln(err)
// }
// for {
// reply := make([]byte, 2048)
// _, err := conn.Read(reply)
// if err != nil {
// g.Close()
// log.Fatalf("Server closed connection\n")
// }
//
// fmt.Fprintln(chatbox, string(reply))
// termbox.Interrupt()
// }
// }
func quit(*gocui.Gui, *gocui.View) error {
return gocui.ErrQuit
}
@ -146,9 +134,6 @@ func sendToServer(g *gocui.Gui, v *gocui.View) error {
if msg == "" {
return nil
}
//////////////7
//////////////7
data.Contents = msg
data.toSend()