Create testing branch and start channels
This commit is contained in:
parent
cd009e8ee6
commit
6da487bc86
|
@ -58,10 +58,14 @@ func server(cmd *cobra.Command) {
|
||||||
ln, err := net.Listen("tcp", ":"+lport)
|
ln, err := net.Listen("tcp", ":"+lport)
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
fmt.Printf("Listening on port %v...\n", lport)
|
fmt.Printf("Listening on port %v...\n", lport)
|
||||||
|
messageChan := make(chan string)
|
||||||
|
// TODO: get channels properly working
|
||||||
|
//receivingChannel := make(chan string)
|
||||||
|
//sendingChannelº := make(chan string)
|
||||||
for {
|
for {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
go handleConn(conn)
|
go handleConn(conn, messageChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -71,14 +75,22 @@ type chatter struct {
|
||||||
IP string
|
IP string
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn, ch chan string) {
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
var otherMessage string
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
otherMessage = <-ch
|
||||||
|
conn.Write([]byte(otherMessage))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
IP := getIP(conn)
|
IP := getIP(conn)
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
||||||
final_message := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP)
|
final_message_connect := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP)
|
||||||
fmt.Print(final_message)
|
fmt.Print(final_message_connect)
|
||||||
conn.Write([]byte("What's your name?\nName: "))
|
conn.Write([]byte("What's your name?\nName: "))
|
||||||
name, err := bufio.NewReader(conn).ReadString('\n')
|
name, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -93,8 +105,10 @@ func handleConn(conn net.Conn) {
|
||||||
fmt.Printf(NewUser.Username + " disconnected!\n")
|
fmt.Printf(NewUser.Username + " disconnected!\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Print("{" + NewUser.IP + "} " + NewUser.Username + ": " + message)
|
finalMessage := fmt.Sprint("{" + NewUser.IP + "} " + NewUser.Username + ": " + message)
|
||||||
conn.Write([]byte(message))
|
fmt.Print(finalMessage)
|
||||||
|
ch <- finalMessage
|
||||||
|
//conn.Write([]byte(message))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue