-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (52 loc) · 1.13 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
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"github.com/adilw3nomad/gopherchat/client"
"github.com/adilw3nomad/gopherchat/clientManager"
)
func main() {
flagMode := flag.String("mode", "server", "Start in client or server mode")
flag.Parse()
if *flagMode == "server" {
startServerMode()
} else {
startClientMode()
}
}
func startServerMode() {
fmt.Println("Starting server...")
listener, error := net.Listen("tcp", ":12345")
if error != nil {
fmt.Println(error)
}
manager := clientManager.NewClientManager()
go manager.Start()
for {
connection, _ := listener.Accept()
if error != nil {
fmt.Println(error)
}
client := &client.Client{Conn: connection, Data: make(chan []byte)}
manager.Register <- client
go manager.Receive(client)
go client.Send()
}
}
func startClientMode() {
fmt.Println("Starting client...")
connection, error := net.Dial("tcp", "localhost:12345")
if error != nil {
fmt.Println(error)
}
client := &client.Client{Conn: connection}
go client.Receive()
for {
reader := bufio.NewReader(os.Stdin)
message, _ := reader.ReadString('\n')
connection.Write([]byte(message))
}
}