MagicOnion icon indicating copy to clipboard operation
MagicOnion copied to clipboard

Question: how does server push notification to client

Open SunHowe opened this issue 5 months ago • 1 comments

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();
            }
        }
    }

SunHowe avatar Sep 05 '24 00:09 SunHowe