forked from centrifugal/centrifuge-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.go
103 lines (82 loc) · 2.6 KB
/
refresh.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
// Demonstrate how to resque from credentials expiration
// (when connection_lifetime set in Centrifugo).
package main
import (
"fmt"
"log"
"time"
"github.com/centrifugal/centrifuge-go"
jwt "github.com/dgrijalva/jwt-go"
)
func connToken(user string, exp int64) string {
// NOTE that JWT must be generated on backend side of your application!
// Here we are generating it on client side only for example simplicity.
claims := jwt.MapClaims{"sub": user}
if exp > 0 {
claims["exp"] = exp
}
t, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte("secret"))
if err != nil {
panic(err)
}
return t
}
type eventHandler struct{}
func (h *eventHandler) OnConnect(c *centrifuge.Client, e centrifuge.ConnectEvent) {
log.Println("Connected")
}
func (h *eventHandler) OnError(c *centrifuge.Client, e centrifuge.ErrorEvent) {
log.Println("Error", e.Message)
}
func (h *eventHandler) OnDisconnect(c *centrifuge.Client, e centrifuge.DisconnectEvent) {
log.Printf("Disconnected, reason: %s, reconnect: %v", e.Reason, e.Reconnect)
}
func (h *eventHandler) OnRefresh(c *centrifuge.Client) (string, error) {
log.Println("Refresh")
// TODO: receive connection token.
token := connToken("113", time.Now().Unix()+10)
return token, nil
}
type subEventHandler struct{}
func (h *subEventHandler) OnPublish(sub *centrifuge.Subscription, e centrifuge.PublishEvent) {
log.Println(fmt.Sprintf("New message received in channel %s: %s", sub.Channel(), string(e.Data)))
}
func (h *subEventHandler) OnSubscribeSuccess(sub *centrifuge.Subscription, e centrifuge.SubscribeSuccessEvent) {
log.Println(fmt.Sprintf("Subscribed to channel channel %s: %#v", sub.Channel(), e))
}
func (h *subEventHandler) OnUnsubscribe(sub *centrifuge.Subscription, e centrifuge.UnsubscribeEvent) {
log.Println(fmt.Sprintf("Unsubscribed from channel channel %s", sub.Channel()))
}
func newConnection() *centrifuge.Client {
wsURL := "ws://localhost:8000/connection/websocket"
c := centrifuge.New(wsURL, centrifuge.DefaultConfig())
c.SetToken(connToken("113", time.Now().Unix()+10))
handler := &eventHandler{}
c.OnDisconnect(handler)
c.OnRefresh(handler)
c.OnConnect(handler)
c.OnError(handler)
err := c.Connect()
if err != nil {
log.Fatalln(err)
}
return c
}
func main() {
log.Println("Start program")
c := newConnection()
defer c.Close()
sub, err := c.NewSubscription("chat:index")
if err != nil {
log.Fatalln(err)
}
eventHandler := &subEventHandler{}
sub.OnPublish(eventHandler)
sub.OnSubscribeSuccess(eventHandler)
sub.OnUnsubscribe(eventHandler)
err = sub.Subscribe()
if err != nil {
log.Fatalln(err)
}
select {}
}