Client tweaks
This commit is contained in:
parent
dff54a8d0b
commit
b4147d6008
|
@ -8,7 +8,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
//"os"
|
||||
)
|
||||
|
||||
// clientCmd represents the client command
|
||||
|
@ -21,10 +21,10 @@ with its server.
|
|||
Example:
|
||||
./tiny-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 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)
|
||||
|
|
|
@ -6,9 +6,11 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jroimartin/gocui"
|
||||
"github.com/nsf/termbox-go"
|
||||
//"time"
|
||||
)
|
||||
|
||||
|
@ -36,7 +38,11 @@ func Client() {
|
|||
defer conn.Close()
|
||||
data.Server = conn
|
||||
|
||||
fmt.Print(receiveMessage(conn))
|
||||
nameRequest, err := receiveMessage(conn)
|
||||
if err != nil {
|
||||
log.Fatalf("Error occurred reading from server while requesting name: %v\n", err)
|
||||
}
|
||||
fmt.Print(nameRequest)
|
||||
sendName(conn)
|
||||
GUI()
|
||||
}
|
||||
|
@ -46,24 +52,31 @@ func Client() {
|
|||
////////////////////////////////////////////////////
|
||||
|
||||
func listenMessages(g *gocui.Gui) {
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(time.Millisecond * 250)
|
||||
chatbox, err := g.View("chatbox")
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
for {
|
||||
messageFromServer := receiveMessage(data.Server)
|
||||
fmt.Fprint(chatbox, messageFromServer)
|
||||
messageFromServer, err := receiveMessage(data.Server)
|
||||
if err != nil {
|
||||
g.Close()
|
||||
log.Fatalf("Error occurred reading from server: %v\n", err)
|
||||
}
|
||||
|
||||
formattedMessage := strings.TrimRight(messageFromServer, "\n")
|
||||
fmt.Fprintln(chatbox, formattedMessage)
|
||||
termbox.Interrupt()
|
||||
}
|
||||
}
|
||||
|
||||
func receiveMessage(conn net.Conn) string {
|
||||
func receiveMessage(conn net.Conn) (s string, err error) {
|
||||
serverMessage := make([]byte, 2048)
|
||||
if _, err := conn.Read(serverMessage); err != nil {
|
||||
log.Fatalf("Error occurred reading from server: %v\n", err)
|
||||
return "", err
|
||||
}
|
||||
finalMessage := string(serverMessage)
|
||||
return finalMessage
|
||||
return finalMessage, nil
|
||||
}
|
||||
|
||||
func sendName(conn net.Conn) {
|
||||
|
|
Loading…
Reference in New Issue