-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-server.go
121 lines (108 loc) · 2.42 KB
/
chat-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bufio"
"fmt"
//"log"
"net"
"os"
//"strconv"
"strings"
)
var connmap = make(map[string]net.Conn)
func createUser(c net.Conn, q string) string {
c.Write([]byte(string(q)))
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return netData
}
username := strings.TrimSpace(string(netData))
if username == "" {
c.Write([]byte(string("You need to enter a username: ")))
} else {
c.Write([]byte(string("Welcome " + username + "\n")))
return username
}
}
c.Close()
return string("testuser")
}
func enterRoom(c net.Conn, username string) {
// tell the user who else is in the room
for key, _ := range connmap {
c.Write([]byte(string("\n" + key + " is in the room\n# ")))
}
// the the other users who has joined the room
for _, value := range connmap {
value.Write([]byte(string("\n" + username + " has joined the room\n# ")))
}
// handle user input
handleCommands(c, username)
}
func handleCommands(c net.Conn, username string) {
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
text := strings.TrimSpace(string(netData))
if text == "" {
c.Write([]byte(string("You need to say something...\n" + "# ")))
} else if text == "QUIT" {
c.Write([]byte(string("Thanks for chatting " + username + " !!\n")))
c.Close()
delete(connmap, username)
} else {
for _, value := range connmap {
value.Write([]byte(string("\n" + username + ": " + text + "\n# ")))
}
}
}
}
func handleConnection(c net.Conn) {
// welcome user
c.Write([]byte(string("Welcome to FlexCHAT !!\n")))
// get user details
username := createUser(c, "Please enter you username: ")
// map username to connection
connmap[username] = c
// enter the chatroom
enterRoom(c, username)
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
text := strings.TrimSpace(string(netData))
if text == "QUIT" {
c.Write([]byte(string("Thanks for chatting !!\n")))
break
}
}
c.Close()
}
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a port number!")
return
}
PORT := ":" + arguments[1]
l, err := net.Listen("tcp4", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(c)
}
}