Disconnect websocket auto cleaning up subscribed channels

This commit is contained in:
2025-03-01 18:02:55 +08:00
parent e3a4988ccf
commit eea9b85745
3 changed files with 31 additions and 6 deletions

View File

@ -6,12 +6,25 @@ import (
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
"git.solsynth.dev/hypernet/messaging/pkg/internal/database"
"git.solsynth.dev/hypernet/messaging/pkg/internal/services"
)
func (v *Server) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.EventResponse, error) {
log.Debug().Str("event", in.GetEvent()).
Msg("Got a broadcasting event...")
switch in.GetEvent() {
// Clear the subscribed channel
case "ws.client.unregister":
// Update user last seen at
data := nex.DecodeMap(in.GetData())
id := data["id"].(string)
services.UnsubscribeAllWithClient(id)
log.Info().Str("client", id).Msg("Client unregistered, cleaning up subscribed channels...")
// Account recycle
case "deletion":
data := nex.DecodeMap(in.GetData())
resType, ok := data["type"].(string)

View File

@ -76,7 +76,7 @@ func (v *Server) PushStream(_ context.Context, request *proto.PushStreamRequest)
action := strings.Split(in.Action, ".")[1]
switch action {
case "subscribe":
services.SubscribeChannel(uint(request.GetUserId()), data.ChannelID)
services.SubscribeChannel(uint(request.GetUserId()), data.ChannelID, request.GetClientId())
case "unsubscribe":
services.UnsubscribeChannel(uint(request.GetUserId()), data.ChannelID)
case "unsubscribeAll":