| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
"os" |
| 8 |
"os/signal" |
| 9 |
"syscall" |
| 10 |
|
| 11 |
_ "net/http/pprof" |
| 12 |
|
| 13 |
"github.com/centrifugal/centrifuge" |
| 14 |
) |
| 15 |
|
| 16 |
func handleLog(e centrifuge.LogEntry) { |
| 17 |
log.Printf("%s: %v", e.Message, e.Fields) |
| 18 |
} |
| 19 |
|
| 20 |
func authMiddleware(h http.Handler) http.Handler { |
| 21 |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 |
ctx := r.Context() |
| 23 |
newCtx := centrifuge.SetCredentials(ctx, ¢rifuge.Credentials{ |
| 24 |
UserID: "42", |
| 25 |
Info: []byte(`{"name": "Alexander"}`), |
| 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 |
cfg.LogLevel = centrifuge.LogLevelInfo |
| 47 |
cfg.LogHandler = handleLog |
| 48 |
|
| 49 |
node, _ := centrifuge.New(cfg) |
| 50 |
|
| 51 |
node.OnConnecting(func(ctx context.Context, e centrifuge.ConnectEvent) (centrifuge.ConnectReply, error) { |
| 52 |
cred, _ := centrifuge.GetCredentials(ctx) |
| 53 |
return centrifuge.ConnectReply{ |
| 54 |
// Subscribe to personal several server-side channel. |
| 55 |
Subscriptions: map[string]centrifuge.SubscribeOptions{ |
| 56 |
"#" + cred.UserID: {Presence: true}, |
| 57 |
}, |
| 58 |
}, nil |
| 59 |
}) |
| 60 |
|
| 61 |
node.OnConnect(func(client *centrifuge.Client) { |
| 62 |
presenceStats, err := node.PresenceStats("#" + client.UserID()) |
| 63 |
if err != nil { |
| 64 |
client.Disconnect(centrifuge.DisconnectServerError) |
| 65 |
return |
| 66 |
} |
| 67 |
if presenceStats.NumClients >= 2 { |
| 68 |
err = node.Disconnect( |
| 69 |
client.UserID(), |
| 70 |
centrifuge.WithDisconnect(centrifuge.DisconnectConnectionLimit), |
| 71 |
centrifuge.WithClientWhitelist([]string{client.ID()}), |
| 72 |
) |
| 73 |
if err != nil { |
| 74 |
client.Disconnect(centrifuge.DisconnectServerError) |
| 75 |
return |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
transport := client.Transport() |
| 80 |
log.Printf("user %s connected via %s with protocol: %s", client.UserID(), transport.Name(), transport.Protocol()) |
| 81 |
|
| 82 |
client.OnDisconnect(func(e centrifuge.DisconnectEvent) { |
| 83 |
log.Printf("user %s disconnected, disconnect: %s", client.UserID(), e.Disconnect) |
| 84 |
}) |
| 85 |
}) |
| 86 |
|
| 87 |
if err := node.Run(); err != nil { |
| 88 |
log.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
websocketHandler := centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{ |
| 92 |
ReadBufferSize: 1024, |
| 93 |
UseWriteBufferPool: true, |
| 94 |
}) |
| 95 |
http.Handle("/connection/websocket", authMiddleware(websocketHandler)) |
| 96 |
|
| 97 |
sockjsHandler := centrifuge.NewSockjsHandler(node, centrifuge.SockjsConfig{ |
| 98 |
URL: "https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js", |
| 99 |
HandlerPrefix: "/connection/sockjs", |
| 100 |
WebsocketReadBufferSize: 1024, |
| 101 |
WebsocketWriteBufferSize: 1024, |
| 102 |
}) |
| 103 |
http.Handle("/connection/sockjs/", authMiddleware(sockjsHandler)) |
| 104 |
http.Handle("/", http.FileServer(http.Dir("./"))) |
| 105 |
|
| 106 |
go func() { |
| 107 |
if err := http.ListenAndServe(":8000", nil); err != nil { |
| 108 |
log.Fatal(err) |
| 109 |
} |
| 110 |
}() |
| 111 |
|
| 112 |
waitExitSignal(node) |
| 113 |
log.Println("bye!") |
| 114 |
} |