Add ability to receive username
This commit is contained in:
parent
ed54642110
commit
15ea54b5ae
|
@ -1,9 +1,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,6 +49,17 @@ func Server() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUsername(conn net.Conn) string {
|
||||||
|
conn.Write([]byte("What's your name?\n\r"))
|
||||||
|
conn.Write([]byte("Choice: "))
|
||||||
|
name, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error occurred getting username: %v\n", err)
|
||||||
|
}
|
||||||
|
trimmedName := strings.TrimRight(name, "\n")
|
||||||
|
return trimmedName
|
||||||
|
}
|
||||||
|
|
||||||
func getUserInput() {
|
func getUserInput() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -54,7 +67,7 @@ func getUserInput() {
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
newUserTemplate := new(User)
|
newUserTemplate := new(User)
|
||||||
newUser := newUserTemplate.CreateUser("Tom", getIP(conn))
|
newUser := newUserTemplate.CreateUser(getUsername(conn), getIP(conn))
|
||||||
fmt.Printf("Received connection from %v at %v\n", newUser.Username, newUser.IP)
|
fmt.Printf("Received connection from %v at %v\n", newUser.Username, newUser.IP)
|
||||||
fmt.Fprintln(conn, "Hello buddy!")
|
fmt.Fprintln(conn, "Hello buddy!")
|
||||||
}
|
}
|
||||||
|
@ -63,6 +76,5 @@ func getIP(conn net.Conn) (IP string) {
|
||||||
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
||||||
IP = fmt.Sprintf("%v", addr.IP)
|
IP = fmt.Sprintf("%v", addr.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
return IP
|
return IP
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue