-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (79 loc) · 1.63 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
package main
import (
"fmt"
"log"
"net"
)
type MessageType int
const (
ClientConnected MessageType = iota + 1
DeleteClient
NewMessage
)
type Message struct {
Type MessageType
Conn net.Conn
Text string
}
func client(conn net.Conn, messages chan Message) {
buffer := make([]byte, 512)
for {
n, err := conn.Read(buffer)
if err != nil {
conn.Close()
messages <- Message{
Type: DeleteClient,
Conn: conn,
}
return
}
messages <- Message{
Type: NewMessage,
Text: string(buffer[0:n]),
Conn: conn,
}
}
}
func server(message chan Message) {
conns := map[string]net.Conn{}
for {
msg := <-message
switch msg.Type {
case ClientConnected:
conns[msg.Conn.RemoteAddr().String()] = msg.Conn
case DeleteClient:
delete(conns, msg.Conn.RemoteAddr().String())
case NewMessage:
for _, conn := range conns {
_, err := conn.Write([]byte(msg.Text))
if err != nil {
// TODO: remove the connection from the list
fmt.Println("Could not send data to ...: %s", err)
}
}
}
}
}
// [null-ls] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("Error ....\n")
}
log.Printf("Listening to TCP connection on port %s ...\n", "8080")
messages := make(chan Message)
go server(messages)
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("ERROR\n")
// handle error
}
log.Printf("accepted connection from %s", conn.RemoteAddr())
messages <- Message{
Type: ClientConnected,
Conn: conn,
}
go client(conn, messages)
}
}