Introduce basic chat functionality
I have yet to develop multi-user chats
This commit is contained in:
parent
7cfd2e87d8
commit
7248c4d30c
|
@ -6,7 +6,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -22,11 +21,6 @@ type User struct {
|
||||||
IP string
|
IP string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Contents string
|
|
||||||
Date time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u User) CreateUser(usr string, ip string) User {
|
func (u User) CreateUser(usr string, ip string) User {
|
||||||
u.Username = usr
|
u.Username = usr
|
||||||
u.IP = ip
|
u.IP = ip
|
||||||
|
@ -49,27 +43,63 @@ func Server() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUsername(conn net.Conn) string {
|
func getUsername(conn net.Conn) (s string, err error) {
|
||||||
conn.Write([]byte("What's your name?\n\r"))
|
conn.Write([]byte("What's your name?\nChoice: "))
|
||||||
conn.Write([]byte("Choice: "))
|
|
||||||
name, err := bufio.NewReader(conn).ReadString('\n')
|
name, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error occurred getting username: %v\n", err)
|
return "", err
|
||||||
}
|
}
|
||||||
trimmedName := strings.TrimRight(name, "\n")
|
trimmedName := strings.TrimRight(name, "\n")
|
||||||
return trimmedName
|
return trimmedName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserInput() {
|
func getUserInput(conn net.Conn) (s string, err error) {
|
||||||
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return message, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
//////////////////////////////////
|
||||||
|
// Get user information
|
||||||
|
//////////////////////////////////
|
||||||
|
userName, err := getUsername(conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error occurred getting username: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userIP := getIP(conn)
|
||||||
|
//////////////////////////////////
|
||||||
|
|
||||||
newUserTemplate := new(User)
|
newUserTemplate := new(User)
|
||||||
newUser := newUserTemplate.CreateUser(getUsername(conn), getIP(conn))
|
newUser := newUserTemplate.CreateUser(userName, userIP)
|
||||||
fmt.Printf("Received connection from %v at %v\n", newUser.Username, newUser.IP)
|
joinMessage := fmt.Sprintf("%v has joined the chat!", newUser.Username)
|
||||||
fmt.Fprintln(conn, "Hello buddy!")
|
fmt.Println(joinMessage)
|
||||||
|
conn.Write([]byte(joinMessage))
|
||||||
|
|
||||||
|
//////////////////////////////////
|
||||||
|
for {
|
||||||
|
message, err := getUserInput(conn)
|
||||||
|
if err != nil {
|
||||||
|
quitMessage := fmt.Sprintf("%v has disconnected!\n", newUser.Username)
|
||||||
|
fmt.Print(quitMessage)
|
||||||
|
if _, err := conn.Write([]byte(quitMessage)); err != nil {
|
||||||
|
log.Printf("Error happened sending disconnect message: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
finalMessage := fmt.Sprintf("[%v] %v: %v", newUser.IP, newUser.Username, strings.TrimRight(message, "\n"))
|
||||||
|
fmt.Printf("%v\n", finalMessage)
|
||||||
|
if _, err := conn.Write([]byte(finalMessage)); err != nil {
|
||||||
|
log.Printf("Error happened sending message: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIP(conn net.Conn) (IP string) {
|
func getIP(conn net.Conn) (IP string) {
|
||||||
|
|
Loading…
Reference in New Issue