-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
73 lines (56 loc) · 1.38 KB
/
client.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
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
type Client struct {
con net.Conn
username string
state ClientRestriction
}
func (client *Client) run(cManager *ClientManager) {
client.state = NONE
con := client.con
// Send message to client
con.Write([]byte("Bienvenue !\n"))
client.login()
line, err2 := bufio.NewReader(con).ReadString('\n')
for line != "" {
if err2 != nil {
fmt.Printf("Error while reading message : %s\n", err2)
return
}
line = strings.ReplaceAll(line, "\n", "")
message := fmt.Sprintf("[CLIENT : %s] %s", client.username, line)
fmt.Print(message)
cManager.sendExcept([]byte(message), Logged, client)
line, err2 = bufio.NewReader(con).ReadString('\n')
}
}
func (client *Client) login() {
con := client.con
client.writeStr("Entrez votre nom d'utilisateur : ")
line, _, err2 := bufio.NewReader(con).ReadLine()
for {
if err2 != nil {
fmt.Printf("[ERROR] Can't read message")
return
}
if len(line) > 0 {
client.username = string(line)
break
}
client.writeStr("Entrez votre nom d'utilisateur : ")
line, _, err2 = bufio.NewReader(con).ReadLine()
}
client.writeStr("Bravo ! tu peux désormais envoyer et recevoir des messages.\n")
client.state = Logged
}
func (client *Client) write(data []byte) {
client.con.Write(data)
}
func (client *Client) writeStr(str string) {
client.con.Write([]byte(str))
}