Final PR #1
|
@ -5,6 +5,7 @@ Copyright © 2024 Raul
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -15,13 +16,10 @@ import (
|
||||||
// clientCmd represents the client command
|
// clientCmd represents the client command
|
||||||
var clientCmd = &cobra.Command{
|
var clientCmd = &cobra.Command{
|
||||||
Use: "client",
|
Use: "client",
|
||||||
Short: "A brief description of your command",
|
Short: "Connect to a mini-chat server",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `Connect to a mini-chat server.
|
||||||
and usage of using your command. For example:
|
Example:
|
||||||
|
./mini-chat client --ip 192.168.0.50 --port 1337`,
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
|
||||||
This application is a tool to generate the needed files
|
|
||||||
to quickly create a Cobra application.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client(cmd)
|
client(cmd)
|
||||||
},
|
},
|
||||||
|
@ -43,13 +41,49 @@ func init() {
|
||||||
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readMsg(conn net.Conn) error {
|
||||||
|
reply := make([]byte, 1024)
|
||||||
|
conn.Read(reply)
|
||||||
|
fmt.Println(string(reply))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func client(cmd *cobra.Command) {
|
func client(cmd *cobra.Command) {
|
||||||
port, _ := cmd.Flags().GetString("port")
|
port, _ := cmd.Flags().GetString("port")
|
||||||
ip, _ := cmd.Flags().GetString("ip")
|
ip, _ := cmd.Flags().GetString("ip")
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
fmt.Println("Not enough arguments, run \"-h\" parameter to see all the parameters!")
|
fmt.Println("Not enough arguments, run \"-h\" parameter to see all the parameters!")
|
||||||
os.Exit(1)
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
if port == "" {
|
||||||
|
port = "1302"
|
||||||
}
|
}
|
||||||
socket := ip + ":" + port
|
socket := ip + ":" + port
|
||||||
net.Dial("tcp", socket)
|
conn, err := net.Dial("tcp", socket)
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// go func() {
|
||||||
|
// for {
|
||||||
|
// reply := make([]byte, 1024)
|
||||||
|
// conn.Read(reply)
|
||||||
|
// fmt.Println(string(reply))
|
||||||
|
// }
|
||||||
|
// }()
|
||||||
|
|
||||||
|
// TODO: GET READING DATA FROM SERVER WORKING
|
||||||
|
|
||||||
|
for {
|
||||||
|
msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
|
cobra.CheckErr(err)
|
||||||
|
conn.Write([]byte(msg))
|
||||||
|
go readMsg(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
|
// fmt.Println(msg)
|
||||||
|
|
||||||
|
//fmt.Println(string(data))
|
||||||
|
|
||||||
|
//ui()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue