olahol / melody

:notes: Minimalist websocket framework for Go
BSD 2-Clause "Simplified" License
3.75k stars 366 forks source link

Make melody.hub public #16

Closed metalmatze closed 7 years ago

metalmatze commented 7 years ago

Hey, I need a way to figure out if a there are active sessions because I only want to do work if there are any. I almost implemented something like the melody.hub myself, but I think that it would be better to make this public or provide some kind of API. Thanks

metalmatze commented 7 years ago

Additionally, when looking at the code for hub{} I think there needs to be a mutex for the map[*Session]bool and for open. 😉

olahol commented 7 years ago

Isn't it possible to do what you want by just using HandleConnect and HandleDisconnect why do you need to access hub?

I don't think a mutex is needed because hub is running in a single go routine.

metalmatze commented 7 years ago

Yes, I can do that outside of this library like:

type SessionCount struct {
    mu    sync.RWMutex
    Count uint
}

But I rather dislike this approach, because it's already handled in the hub. Maybe we can add aLen() int. Also GetSession() would be nice, but not mandatory for me.

olahol commented 7 years ago

I think a Len() method would be a great idea. I will add one this week.

olahol commented 7 years ago

Len() method is now added to Melody. Thanks @metalmatze .