/
client.go
82 lines (76 loc) · 1.43 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
package main
import (
_ "bufio"
"bytes"
"fmt"
"github.com/gorilla/websocket"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"sync"
"time"
)
var C = 1000
//var sout = bufio.NewWriter(os.Stdout)
var m = &sync.Mutex{}
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
wg := &sync.WaitGroup{}
for i := 0; i < C; i++ {
wg.Add(1)
go connect(i, wg)
}
wg.Wait()
}
func connect(cid int, wg *sync.WaitGroup) {
conn, err := net.Dial("tcp", "127.0.0.1:4000")
if err != nil {
fmt.Println(err)
return
}
u, err := url.Parse("http://127.0.0.1:4000/foobar")
if err != nil {
fmt.Println(err)
return
}
h := &http.Header{}
h.Add("Origin", "localhost:4000")
sc, _, err := websocket.NewClient(conn, u, *h, 0, 0)
if err != nil {
fmt.Println(err)
return
}
defer sc.Close()
var mid = 0
for {
time.Sleep(1 * time.Nanosecond)
if err = sc.WriteMessage(1, []byte("Hello from client")); err != nil {
fmt.Println(err)
return
}
_, p, err := sc.ReadMessage()
if err != nil {
fmt.Println(err)
return
}
//m.Lock()
sout := bytes.NewBuffer(make([]byte, 0))
fmt.Fprintf(sout, "[c-%v][m-%v] RCVD:", cid, mid)
fmt.Fprintf(sout, "%v\n", string(p))
//m.Unlock()
ioutil.WriteFile(fmt.Sprintf("results/%v.%v", cid, mid), sout.Bytes(), os.ModePerm)
mid++
if mid == 100 {
return
}
}
time.Sleep(5 * time.Second)
// sout.Flush()
time.Sleep(5 * time.Second)
wg.Done()
}