golang-exercises/chat-tests/cmd/clientFunc.go

196 lines
3.9 KiB
Go
Raw Normal View History

2024-04-25 10:24:27 +02:00
/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
2024-04-23 08:00:07 +02:00
package cmd
import (
2024-04-23 09:58:07 +02:00
"bufio"
"fmt"
2024-04-25 10:24:27 +02:00
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
2024-04-23 09:58:07 +02:00
"log"
"net"
"os"
2024-04-24 16:14:44 +02:00
"strings"
2024-04-23 09:58:07 +02:00
"time"
2024-04-23 08:00:07 +02:00
)
2024-04-23 09:58:07 +02:00
type Message struct {
Contents string
//Date time.Time
Server net.Conn
}
func (m Message) toSend() {
m.Server.Write([]byte(m.Contents))
}
2024-04-23 08:00:07 +02:00
var (
serverPort string = "1302"
serverIP string
2024-04-23 09:58:07 +02:00
data Message
2024-04-23 08:00:07 +02:00
)
func Client() {
2024-04-23 09:58:07 +02:00
conn, err := net.Dial("tcp", serverIP+":"+serverPort)
if err != nil {
log.Fatalf("Error occurred trying to connect to server: %v\n", err)
}
defer conn.Close()
data.Server = conn
2024-04-24 16:14:44 +02:00
nameRequest, err := receiveMessage(conn)
if err != nil {
log.Fatalf("Error occurred reading from server while requesting name: %v\n", err)
}
fmt.Print(nameRequest)
2024-04-23 09:58:07 +02:00
sendName(conn)
GUI()
}
////////////////////////////////////////////////////
// REFACTORING BELOW //
////////////////////////////////////////////////////
func listenMessages(g *gocui.Gui) {
2024-04-24 16:14:44 +02:00
time.Sleep(time.Millisecond * 250)
2024-04-23 09:58:07 +02:00
chatbox, err := g.View("chatbox")
if err != nil {
log.Panicln(err)
}
for {
2024-04-24 16:14:44 +02:00
messageFromServer, err := receiveMessage(data.Server)
if err != nil {
2024-04-25 10:24:27 +02:00
// 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)
}
2024-04-24 16:14:44 +02:00
}
formattedMessage := strings.TrimRight(messageFromServer, "\n")
fmt.Fprintln(chatbox, formattedMessage)
termbox.Interrupt()
2024-04-23 09:58:07 +02:00
}
}
2024-04-24 16:14:44 +02:00
func receiveMessage(conn net.Conn) (s string, err error) {
2024-04-23 09:58:07 +02:00
serverMessage := make([]byte, 2048)
if _, err := conn.Read(serverMessage); err != nil {
2024-04-24 16:14:44 +02:00
return "", err
2024-04-23 09:58:07 +02:00
}
finalMessage := string(serverMessage)
2024-04-24 16:14:44 +02:00
return finalMessage, nil
2024-04-23 09:58:07 +02:00
}
func sendName(conn net.Conn) {
message, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
log.Fatalf("Error occurred sending message to server: %v\n", err)
}
if _, err := conn.Write([]byte(message)); err != nil {
log.Fatalf("Error occurred writing to server: %v\n", err)
}
}
func GUI() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
g.Mouse = true
g.Cursor = true
initKeybindings(g)
go listenMessages(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func quit(*gocui.Gui, *gocui.View) error {
return gocui.ErrQuit
}
func sendToServer(g *gocui.Gui, v *gocui.View) error {
textarea, err := g.View("textarea")
if err != nil {
log.Panicln(err)
}
message := textarea.Buffer()
msg := string(message)
if msg == "" {
return nil
}
if msg == "" {
return nil
}
data.Contents = msg
data.toSend()
textarea.Clear()
textarea.SetCursor(0, 0)
return nil
}
func initKeybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("button", gocui.MouseLeft, gocui.ModNone, sendToServer); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("textarea", gocui.KeyEnter, gocui.ModNone, sendToServer); err != nil {
log.Panicln(err)
}
return nil
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if chatbox, err := g.SetView("chatbox", 2, 1, maxX/2+40, maxY-5); err != nil {
if err != gocui.ErrUnknownView {
return err
}
chatbox.Autoscroll = true
chatbox.Title = "Chat Box"
}
if button, err := g.SetView("button", maxX/2+32, maxY-4, maxX/2+40, maxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
button.Wrap = true
button.Frame = true
fmt.Fprintln(button, "Send it")
}
if textarea, err := g.SetView("textarea", 2, maxY-4, maxX/2+28, maxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
if _, err := g.SetCurrentView("textarea"); err != nil {
log.Panicln(err)
}
textarea.Title = "Send message"
textarea.Wrap = true
textarea.Editable = true
}
2024-04-23 08:00:07 +02:00
2024-04-23 09:58:07 +02:00
return nil
2024-04-23 08:00:07 +02:00
}