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 ( import (
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log" "os"
//"os"
) )
// clientCmd represents the client command // clientCmd represents the client command
var clientCmd = &cobra.Command{ var clientCmd = &cobra.Command{
Use: "client", Use: "client",
Short: "Client interface for tiny-chat", Short: "Client interface for mini-chat",
Long: `Refactored mini-chat client that properly interfaces Long: `Refactored mini-chat client that properly interfaces
with its server. with its server.
Example: 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) { Run: func(cmd *cobra.Command, args []string) {
// if len(args) == 0 {
// cmd.Help()
// os.Exit(0)
// }
if err := setClientParameters(cmd); err != nil { 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() Client()

View File

@ -1,17 +1,19 @@
/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
package cmd package cmd
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
"log" "log"
"net" "net"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
//"time"
) )
type Message struct { type Message struct {
@ -60,8 +62,13 @@ func listenMessages(g *gocui.Gui) {
for { for {
messageFromServer, err := receiveMessage(data.Server) messageFromServer, err := receiveMessage(data.Server)
if err != nil { if err != nil {
g.Close() // Avoid triggering an error if client quits the client
log.Fatalf("Error occurred reading from server: %v\n", err) if err == gocui.ErrQuit {
g.Close()
} else {
g.Close()
log.Fatalf("Error occurred reading from server: %v\n", err)
}
} }
formattedMessage := strings.TrimRight(messageFromServer, "\n") formattedMessage := strings.TrimRight(messageFromServer, "\n")
@ -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 { func quit(*gocui.Gui, *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit
} }
@ -146,9 +134,6 @@ func sendToServer(g *gocui.Gui, v *gocui.View) error {
if msg == "" { if msg == "" {
return nil return nil
} }
//////////////7
//////////////7
data.Contents = msg data.Contents = msg
data.toSend() data.toSend()