-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
56 lines (47 loc) · 857 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
53
54
55
56
package main
import (
"fmt"
"time"
"math/rand"
)
type Message struct {
Name string `json:"name"`
}
type Client struct{
send chan Message
}
func (client *Client) write(){
// range keword used for pull single value from client.send
for msg := range client.send {
fmt.Printf("%#v\n", msg)
}
}
/**
* Generates a random number between 0 and 10
*/
func r() time.Duration{
return time.Second * time.Duration(rand.Intn(10))
}
func (client *Client) subscribeChannels(){
for {
time.Sleep(r())
client.send <- Message{"channel add"}
}
}
func (client *Client) subscribeMessages(){
for {
time.Sleep(r())
client.send <- Message{"message add"}
}
}
func NewClient() *Client{
return &Client{
send: make(chan Message),
}
}
func main(){
client := NewClient()
go client.subscribeChannels()
go client.subscribeMessages()
client.write()
}