-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (53 loc) · 1.19 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"github.com/alvindaiyan/goTalk2/client_app/client"
"github.com/alvindaiyan/goTalk2/client_app/server"
)
var (
msgc = make(chan string) // the message channel
serverAddr = flag.String("server_addr", "127.0.0.1:10000", "The server address in the format of host:port")
myTitle = flag.String("title", "", "The name show to your friend")
)
// an input from command line
func input() {
for {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
msgc <- text
}
}
func main() {
flag.Parse()
fmt.Println("start the program")
for {
// start the app
waitc := make(chan struct{}) // a wait lock
// start the server thread
go func() {
fmt.Println("start the server")
server.InitChatServer()
defer close(waitc)
}()
// start the client thread
go func() {
for {
msg := <-msgc // a message to send
client.InitChatClient(*myTitle, serverAddr)
err := client.Chat(msg)
if err != nil {
// restart the client
fmt.Printf("send Err: %v", err)
}
}
}()
// start the input thread
go input()
<-waitc
// finished in this round restart the app
fmt.Println("restart the app")
}
}