-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
106 lines (88 loc) · 2.2 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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"time"
"github.com/containerd/console"
)
var name string
func clearPrevLine() {
fmt.Print("\033[A\033[2KT\r")
}
func clearCurLine() {
fmt.Print("\033[2K\r")
}
func clearScreen() {
fmt.Print("\033c")
}
func printLineAndPrompt(line string) {
clearCurLine()
println(line)
fmt.Print("Say -> ")
}
func LaunchClient() {
//use external lib to setup windows terminal
//will not work in debug mode
console.Current()
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("Enter your name: ")
re := regexp.MustCompile(`^[A-Za-z0-9_]{3,}$`)
for {
scanner.Scan()
text := scanner.Text()
if re.MatchString(text) {
name = text
break
}
clearPrevLine()
fmt.Printf("Try again: ")
}
clearScreen()
go getUpdatesLoop()
for {
fmt.Print("Say -> ")
scanner.Scan()
text := scanner.Text()
clearPrevLine()
if len(text) != 0 {
data := PostMessageRequest{
From: name,
Text: text,
}
js, _ := json.Marshal(&data)
http.Post("http://localhost:14300/postMessage", "application/json", bytes.NewBuffer(js))
} else {
// exit if user entered an empty string
break
}
}
}
func getUpdatesLoop() {
for {
printLineAndPrompt(fmt.Sprintf("[%v] Sent getUpdates request to server", time.Now().Format("15:04:05.000")))
resp, err := http.Get(fmt.Sprintf("http://localhost:14300/getUpdates?for=%v", name))
if err != nil {
printLineAndPrompt(fmt.Sprintf("[%v] Received error on getUpdates from server", time.Now().Format("15:04:05.000")))
time.Sleep(10 * time.Second)
} else {
var reply GetUpdatesReply
dec := json.NewDecoder(resp.Body)
dec.DisallowUnknownFields()
err = dec.Decode(&reply)
if err != nil {
printLineAndPrompt(fmt.Sprintf("[%v] Received invalid getUpdates reply from server", time.Now().Format("15:04:05.000")))
} else {
printLineAndPrompt(fmt.Sprintf("[%v] Received correct getUpdates reply from server", time.Now().Format("15:04:05.000")))
for _, msg := range reply.Messages {
printLineAndPrompt(fmt.Sprintf("[%v]->[%v] %v: %v", msg.Time.Format("15:04:05.000"), time.Now().Format("15:04:05.000"), msg.From, msg.Text))
}
}
resp.Body.Close()
}
}
}