-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
52 lines (43 loc) · 895 Bytes
/
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
package main
import (
"flag"
"fmt"
"log"
"strconv"
"golang.org/x/net/websocket"
)
const (
ORIGIN = "http://go-websocket-test"
)
func main() {
url := flag.String("url", "", "url of the websocket server")
connections := flag.Int("connections", 10000, "number of connections")
flag.Parse()
connectionChan := make(chan *websocket.Conn)
wsMap := make(map[int]*websocket.Conn)
// Go routine to read connections from my connection channel (thread safe)
go func() {
i := 0
for {
select {
case ws := <-connectionChan:
wsMap[i] = ws
i++
fmt.Println("Connection " + strconv.Itoa(i))
}
}
}()
for i := 0; i < *connections; i++ {
go func() {
var err error
ws, err := websocket.Dial(*url, "", ORIGIN)
if err != nil {
fmt.Println("Error connecting...")
log.Fatal(err)
}
connectionChan <- ws
}()
}
quit := make(chan bool)
<-quit
}