-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
147 lines (128 loc) · 3.31 KB
/
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"chat-server-private-message/config"
"errors"
"fmt"
"net"
"strings"
)
type Client struct {
Connection net.Conn
Username string
Friend string
Configs config.Configs
}
type Message struct {
SenderUsername string
ReceiverUsername string
Text string
}
var clients []*Client
func main() {
configs := config.LoadConfig()
psock, err := net.Listen("tcp", ":"+configs.Port)
config.CheckForError(err, "Server can't start.")
fmt.Printf("start... port %v \n", configs.Port)
for {
conn, _ := psock.Accept()
client := Client{Connection: conn, Configs: configs}
client.Register()
var msg Message
channel := make(chan string)
go getInput(channel, &client)
go sendResponse(channel, &client, &msg)
SendMessageClient("start", configs.Port, &client, "")
}
}
func getInput(send chan string, client *Client) {
defer close(send)
reader := bufio.NewReader(client.Connection)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
fmt.Println("err", err)
client.Close()
return
}
send <- string(line)
}
}
func sendResponse(receive <-chan string, client *Client, msg *Message) {
for {
message := <-receive
message = strings.TrimSpace(message)
cmd, body := getCommandParts(message)
if cmd != "" {
switch cmd {
case "new":
err := client.checkUsername(body)
if err != nil {
SendMessageClient("error", body, client, "")
client.Close()
return
}
client.Username = body
msg.SenderUsername = client.Username
SendMessageClient("connect", body, client, "")
case "message":
if msg.SenderUsername == "" {
SendMessageClient("getUsername", body, client, "")
} else if msg.ReceiverUsername == "" {
msg.ReceiverUsername = body
SendMessageClient("getMessage", body, client, "")
} else if msg.Text == "" {
msg.Text = body
SendMessageClient("sendMessage", body, client, msg.ReceiverUsername)
SendMessageClient("getUsername", body, client, "")
msg.ReceiverUsername = ""
msg.Text = ""
} else {
msg.ReceiverUsername = ""
msg.Text = ""
SendMessageClient("getUsername", body, client, "")
}
}
}
}
}
func getCommandParts(message string) (string, string) {
s := strings.Split(message, " ")
if len(s) > 0 {
result := strings.Join(s[1:], " ")
return strings.Replace(s[0], "/", "", 1), result
}
return "", ""
}
func SendMessageClient(messageType string, message string, client *Client, friend string) {
for _, _client := range clients {
if _client.Username == friend && friend != "" {
payload := fmt.Sprintf("/%v %v %v", messageType, client.Username, message)
fmt.Fprintln(_client.Connection, payload)
break
} else if _client.Username == client.Username && messageType != "sendMessage" {
payload := fmt.Sprintf("/%v %v %v", messageType, client.Username, message)
fmt.Fprintln(_client.Connection, payload)
break
}
}
}
func (client *Client) Register() {
clients = append(clients, client)
}
func (client *Client) Close() {
client.Connection.Close()
for i := 0; i < len(clients); i++ {
if client == clients[i] {
clients = append(clients[:i], clients[i+1:]...)
}
}
}
func (client *Client) checkUsername(username string) error {
for _, c := range clients {
if c.Username == username {
return errors.New("error")
}
}
return nil
}