-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (92 loc) · 2.48 KB
/
main.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
package main
import (
"log"
"net"
"time"
)
type MessageType int
type Message struct {
Conn net.Conn
Type MessageType
Text []byte
}
const Port = "9595"
const (
NewClient MessageType = iota
DisconnectedClient
NewTextClient
)
func main() {
ln := startServer()
channel := make(chan Message)
go Chat(channel)
for {
conn, err := ln.Accept()
if err != nil {
log.Println("Could not accept connection. ", err)
}
channel <- NewMessage(conn, NewClient, nil)
log.Println("connection accepted. ", conn.RemoteAddr())
go HandleConnection(conn, channel)
}
}
func HandleConnection(conn net.Conn, channel chan Message) {
for {
buffer := make([]byte, 512)
_, err := conn.Read(buffer)
if err != nil {
channel <- NewMessage(conn, DisconnectedClient, nil)
break
}
channel <- NewMessage(conn, NewTextClient, buffer)
}
}
func Chat(broadcastChan chan Message) {
clients := make(map[string]net.Conn)
lastNewTextClient := make(map[string]time.Time)
for {
msg := <-broadcastChan
if msg.Type == NewClient {
clients[msg.Conn.RemoteAddr().String()] = msg.Conn
log.Println("New client = ", msg.Conn.RemoteAddr().String())
} else if msg.Type == DisconnectedClient {
delete(clients, msg.Conn.RemoteAddr().String())
msg.Conn.Close()
log.Println("Client disconnected. Connection closed.")
} else if msg.Type == NewTextClient {
lastTime := lastNewTextClient[msg.Conn.RemoteAddr().String()]
if !lastTime.IsZero() && lastTime.After(time.Now().Add(-time.Second*5)) {
msg.Conn.Write([]byte("The time elapse between messages is 5 seconds."))
} else {
lastNewTextClient[msg.Conn.RemoteAddr().String()] = time.Now()
for _, conn := range clients {
if conn.RemoteAddr().String() == msg.Conn.RemoteAddr().String() {
continue
}
conn.Write(msg.Text)
}
}
} else {
log.Println("Unknown message type received = ", msg.Type)
}
}
}
func NewMessage(conn net.Conn, msgType MessageType, buffer []byte) Message {
if msgType == NewClient {
return Message{Conn: conn, Type: NewClient}
} else if msgType == DisconnectedClient {
return Message{Conn: conn, Type: DisconnectedClient}
} else if msgType == NewTextClient {
return Message{Conn: conn, Type: NewTextClient, Text: buffer}
} else {
return Message{Conn: conn}
}
}
func startServer() net.Listener {
ln, err := net.Listen("tcp", ":"+Port)
if err != nil {
log.Fatalf("Could not listen on port %s. Shutting down ...\n", Port)
}
log.Printf("Listening on port %s\n", Port)
return ln
}