Cysharp / MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
MIT License
3.89k stars 433 forks source link

Question: how does server push notification to client #831

Open SunHowe opened 2 months ago

SunHowe commented 2 months ago

Can I only push messages from the server to a client in the following way? Is any way to push notification without Group.AddAsync ?


    public interface IGameHub : IStreamingHub<IGameHub, IGameHubReceiver>{}

    public interface IGameHubReceiver
    {
        void OnReceiveMessage(Message message);
    }

    public sealed class GameHub : StreamingHubBase<IGameHub, IGameHubReceiver>, IGameHub
    {
        private IGroup group;
        private bool isConnected = false;

        protected override async ValueTask OnConnected()
        {
            isConnected = true;
            group = await Group.AddAsync("Global");
        }

        protected override ValueTask OnDisconnected()
        {
            isConnected = false;
            return ValueTask.CompletedTask;
        }

        private async ValueTask LoopLogic()
        {
            while (isConnected)
            {
                BroadcastToSelf(group).OnReceiveMessage("Some message");

                await Task.Yield();
            }
        }
    }
licentia88 commented 1 month ago

well what happens in the OnConnected method is that you are actually subscribing to a group named global and than you can broadcast to entire group, or to specific individual(s), so yes, you need use Group.AddAsync method.

what kind of subscribe/publish are you looking for ? Perhaps the technology you are looking for is

https://github.com/Cysharp/MessagePipe