Skip to content

Instantly share code, notes, and snippets.

@FZambia
Last active March 8, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FZambia/5d72e3acccbb00f4d775b2cef97210a3 to your computer and use it in GitHub Desktop.
Save FZambia/5d72e3acccbb00f4d775b2cef97210a3 to your computer and use it in GitHub Desktop.
Centrifuge library small example
package main
import (
"context"
"log"
"net/http"
"github.com/centrifugal/centrifuge"
)
func main() {
node := centrifuge.New(centrifuge.DefaultConfig)
handleRPC := func(ctx context.Context, req *centrifuge.RPCContext) (*centrifuge.RPCReply, error) {
return &centrifuge.RPCReply{
Result: []byte(`{"data": "response"}`),
}, nil
}
handleConnect := func(ctx context.Context, req *centrifuge.ConnectContext) (*centrifuge.ConnectReply, error) {
log.Printf("user %s connected", req.Client.UserID())
return nil, nil
}
handleDisconnect := func(ctx context.Context, req *centrifuge.DisconnectContext) (*centrifuge.DisconnectReply, error) {
log.Printf("user %s disconnected", req.Client.UserID())
return nil, nil
}
mediator := &centrifuge.Mediator{
RPC: handleRPC,
Connect: handleConnect,
Disconnect: handleDisconnect,
}
node.SetMediator(mediator)
if err := node.Run(); err != nil {
panic(err)
}
http.Handle("/connection/websocket", centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{}))
if err := http.ListenAndServe(":8000", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment