0

How do send subscribers information asynchronously?

RecievMessage-Callback method

Contract

    [ServiceContract(CallbackContract = typeof(IChatClient))]
    public interface IChatService
    {
        [OperationContract(IsOneWay = true)]
        void Join(string username);
        [OperationContract(IsOneWay = true)]
        void SendMessage(byte[] audio, string name);
        [OperationContract]
        List<string> GetUserList();
    }
    [ServiceContract]
    public interface IChatClient
    {
        [OperationContract(IsOneWay = true)]
        void RecievMessage(string user, string message);
    }

Service

        Dictionary<IChatClient, string> _users = new Dictionary<IChatClient, string>();       
        public void Join(string username)
        {
            var connection = OperationContext.Current.GetCallbackChannel<IChatClient>();
            _users[connection] = username;
            list.Add(connection);
        }
        public void SendMessage(string message, string name)
        {
            var connection = OperationContext.Current.GetCallbackChannel<IChatClient>();
            string user;
            if (!_users.TryGetValue(connection, out user))
                return;
            foreach (var other in _users.Keys)
            {
                if (other == connection)
                    continue;
                other.RecievMessage(user, message);
            }
        }

Without asynchrony, the service cannot cope

How make the SendMessage method asynchronous ?

  • Found that the SendMessage method parameters of your two parts are inconsistent. About asynchronous can refer to:https://stackoverflow.com/questions/5979252/wcf-asynchronous-callback and https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-implement-an-asynchronous-service-operation – Lan Huang Dec 01 '21 at 08:09

0 Answers0