centrifugal/centrifuge-go

OnLeave and OnJoin does not work

korvinko opened this issue · 1 comments

This is example of my code:

package main

// Demonstrate how to resque from credentials expiration (when connection_lifetime set in Centrifugo).

import (
    //"fmt"
    "log"

    "github.com/centrifugal/centrifuge-go"
    "github.com/centrifugal/centrifugo/libcentrifugo/auth"
    "fmt"
)

func credentials() *centrifuge.Credentials {
    // Never show secret to client of your application. Keep it on your application backend only.
    secret := "zzzz"

    // Application user ID.
    user := "999"

    // Current timestamp as string.
    timestamp := centrifuge.Timestamp()

    // Empty info.
    info := ""

    // Generate client token so Centrifugo server can trust connection parameters received from client.
    token := auth.GenerateClientToken(secret, user, timestamp, info)

    return &centrifuge.Credentials{
        User:      user,
        Timestamp: timestamp,
        Info:      info,
        Token:     token,
    }
}

func newConnection(done chan struct{}) centrifuge.Centrifuge {
    creds := credentials()
    wsURL := "wss://crm.loc:8000/connection/websocket"

    events := &centrifuge.EventHandler{
        OnDisconnect: func(c centrifuge.Centrifuge) error {
            log.Println("Disconnected")
            close(done)
            return nil
        },
        OnRefresh: func(c centrifuge.Centrifuge) (*centrifuge.Credentials, error) {
            log.Println("Refresh")
            return credentials(), nil
        },
    }

    c := centrifuge.NewCentrifuge(wsURL, creds, events, centrifuge.DefaultConfig)

    err := c.Connect()
    if err != nil {
        log.Fatalln(err)
    }

    log.Println("Connected")

    onLeave := func(sub centrifuge.Sub, msg centrifuge.ClientInfo) error {
        log.Println("Leave")
        //log.Println(fmt.Sprintf("User %s with clientID left channel %s with client ID %s", msg.User, msg.Client, sub.Channel()))
        return nil
    }

    onMessage := func(sub centrifuge.Sub, msg centrifuge.Message) error {
        log.Println(fmt.Sprintf("New message received in channel %s: %#v", sub.Channel(), msg))
        return nil
    }


    subEvents := &centrifuge.SubEventHandler{
        OnLeave: onLeave,
        OnMessage: onMessage,
        OnJoin: onLeave,
    }

    channelName := "develop.system-events"

    _, err = c.Subscribe(channelName, subEvents)
    if err != nil {
        log.Fatalln(err)
    }

    return c
}

func main() {
    log.Println("Start program")
    done := make(chan struct{})
    c := newConnection(done)
    defer c.Close()
    <-done
}

OnLeave and OnJoin does not work. Nothing happens when I leave and join in same channel "develop.system-events". In web interface I sow that user connected and disconnected but client do not catch it.
OnMessage eventHandler works ok.

My mistake just need provide

join_leave set true in config.json