| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
"os" |
| 8 |
"os/signal" |
| 9 |
"syscall" |
| 10 |
"time" |
| 11 |
|
| 12 |
_ "net/http/pprof" |
| 13 |
|
| 14 |
"github.com/centrifugal/centrifuge" |
| 15 |
) |
| 16 |
|
| 17 |
func handleLog(e centrifuge.LogEntry) { |
| 18 |
log.Printf("%s: %v", e.Message, e.Fields) |
| 19 |
} |
| 20 |
|
| 21 |
func authMiddleware(h http.Handler) http.Handler { |
| 22 |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 |
ctx := r.Context() |
| 24 |
newCtx := centrifuge.SetCredentials(ctx, ¢rifuge.Credentials{ |
| 25 |
UserID: "42", |
| 26 |
}) |
| 27 |
r = r.WithContext(newCtx) |
| 28 |
h.ServeHTTP(w, r) |
| 29 |
}) |
| 30 |
} |
| 31 |
|
| 32 |
func waitExitSignal(n *centrifuge.Node) { |
| 33 |
sigCh := make(chan os.Signal, 1) |
| 34 |
done := make(chan bool, 1) |
| 35 |
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 36 |
go func() { |
| 37 |
<-sigCh |
| 38 |
_ = n.Shutdown(context.Background()) |
| 39 |
done <- true |
| 40 |
}() |
| 41 |
<-done |
| 42 |
} |
| 43 |
|
| 44 |
func main() { |
| 45 |
cfg := centrifuge.DefaultConfig |
| 46 |
|
| 47 |
cfg.LogLevel = centrifuge.LogLevelInfo |
| 48 |
cfg.LogHandler = handleLog |
| 49 |
|
| 50 |
node, _ := centrifuge.New(cfg) |
| 51 |
|
| 52 |
broker, _ := centrifuge.NewMemoryBroker(node, centrifuge.MemoryBrokerConfig{ |
| 53 |
HistoryMetaTTL: 120 * time.Second, |
| 54 |
}) |
| 55 |
node.SetBroker(broker) |
| 56 |
|
| 57 |
node.OnConnecting(func(ctx context.Context, e centrifuge.ConnectEvent) (centrifuge.ConnectReply, error) { |
| 58 |
cred, _ := centrifuge.GetCredentials(ctx) |
| 59 |
return centrifuge.ConnectReply{ |
| 60 |
Data: []byte(`{}`), |
| 61 |
// Subscribe to personal several server-side channel. |
| 62 |
Subscriptions: map[string]centrifuge.SubscribeOptions{ |
| 63 |
"#" + cred.UserID: {}, |
| 64 |
}, |
| 65 |
}, nil |
| 66 |
}) |
| 67 |
|
| 68 |
node.OnConnect(func(client *centrifuge.Client) { |
| 69 |
// Declare concurrency semaphore for client connection closure. |
| 70 |
semaphore := make(chan struct{}, 16) |
| 71 |
|
| 72 |
client.OnAlive(func() { |
| 73 |
log.Printf("user %s connection is still active", client.UserID()) |
| 74 |
}) |
| 75 |
|
| 76 |
client.OnSubscribe(func(e centrifuge.SubscribeEvent, cb centrifuge.SubscribeCallback) { |
| 77 |
log.Printf("user %s subscribes on %s", client.UserID(), e.Channel) |
| 78 |
semaphore <- struct{}{} |
| 79 |
go func() { |
| 80 |
defer func() { <-semaphore }() |
| 81 |
time.Sleep(200 * time.Millisecond) |
| 82 |
cb(centrifuge.SubscribeReply{}, nil) |
| 83 |
}() |
| 84 |
}) |
| 85 |
|
| 86 |
client.OnUnsubscribe(func(e centrifuge.UnsubscribeEvent) { |
| 87 |
log.Printf("user %s unsubscribed from %s", client.UserID(), e.Channel) |
| 88 |
}) |
| 89 |
|
| 90 |
client.OnRPC(func(e centrifuge.RPCEvent, cb centrifuge.RPCCallback) { |
| 91 |
log.Printf("RPC from user: %s, data: %s, method: %s", client.UserID(), string(e.Data), e.Method) |
| 92 |
semaphore <- struct{}{} |
| 93 |
go func() { |
| 94 |
defer func() { <-semaphore }() |
| 95 |
time.Sleep(100 * time.Millisecond) |
| 96 |
cb(centrifuge.RPCReply{Data: []byte(`{"year": "2020"}`)}, nil) |
| 97 |
}() |
| 98 |
}) |
| 99 |
|
| 100 |
client.OnDisconnect(func(e centrifuge.DisconnectEvent) { |
| 101 |
log.Printf("user %s disconnected, disconnect: %s", client.UserID(), e.Disconnect) |
| 102 |
}) |
| 103 |
|
| 104 |
transport := client.Transport() |
| 105 |
log.Printf("user %s connected via %s with protocol: %s", client.UserID(), transport.Name(), transport.Protocol()) |
| 106 |
}) |
| 107 |
|
| 108 |
if err := node.Run(); err != nil { |
| 109 |
log.Fatal(err) |
| 110 |
} |
| 111 |
|
| 112 |
websocketHandler := centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{ |
| 113 |
ReadBufferSize: 1024, |
| 114 |
UseWriteBufferPool: true, |
| 115 |
}) |
| 116 |
http.Handle("/connection/websocket", authMiddleware(websocketHandler)) |
| 117 |
|
| 118 |
sockjsHandler := centrifuge.NewSockjsHandler(node, centrifuge.SockjsConfig{ |
| 119 |
URL: "https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js", |
| 120 |
HandlerPrefix: "/connection/sockjs", |
| 121 |
WebsocketReadBufferSize: 1024, |
| 122 |
WebsocketWriteBufferSize: 1024, |
| 123 |
}) |
| 124 |
http.Handle("/connection/sockjs/", authMiddleware(sockjsHandler)) |
| 125 |
|
| 126 |
http.Handle("/", http.FileServer(http.Dir("./"))) |
| 127 |
|
| 128 |
go func() { |
| 129 |
if err := http.ListenAndServe(":8000", nil); err != nil { |
| 130 |
log.Fatal(err) |
| 131 |
} |
| 132 |
}() |
| 133 |
|
| 134 |
waitExitSignal(node) |
| 135 |
log.Println("bye!") |
| 136 |
} |