Share via

Asynchronous message sending WCF

Naydachy 1 Reputation point
2021-11-25T18:44:03.377+00:00

The addMessage method asynchronously sends messages to clients.

The problem is that those who send it receive it themselves

That is, the client hears himself

    public void AddMessage(byte[] message)
    {

          IAsyncMessageCallback other = OperationContext.Current.GetCallbackChannel<IAsyncMessageCallback>();
          other.BeginOnMessageAdded(message, DateTime.Now, delegate (IAsyncResult ar)
                {
                    other.EndOnMessageAdded(ar);
                }, null);


    }

I tried to implement it in the following way:

 public void AddMessage(byte[] message)
        {
            lock (subscribers)
            {
                var connection = OperationContext.Current.GetCallbackChannel<IAsyncMessageCallback>();

                string user;
                if (!subscribers.TryGetValue(connection, out user))
                    return;
                foreach (var other in subscribers.Keys)
                {
                    //Console.WriteLine(other + " " + connection);
                    if (other == connection)
                        continue;
                    Console.WriteLine(other + " " + connection);
                    other.BeginOnMessageAdded(message, DateTime.Now, delegate (IAsyncResult ar)
                    {
                        other.EndOnMessageAdded(ar);
                    }, null);
                    Console.WriteLine(message.Length);
                }
            }
          }

But this leads to terrible brakes

Thank you in advance

Developer technologies | .NET | Other

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,221 Reputation points Microsoft External Staff
    2021-11-30T10:01:15.297+00:00

    Hi @Naydachy ,
    You want to publish the last received message to every client except the one who sent it, you would:

    lock (subscribers)  
            {  
                foreach (var _subscriber in subscribers)  
                {  
                    if (OperationContext.Current.GetCallbackChannel<IPriceChangeNotification>() == _subscriber.Key)  
                    {  
                          //if the person who sent the last message is the current subscriber, there is no need to  
                          //publish the message to him, so skip this iteration  
                            continue;  
                    }  
                    else  
                    {  
                           //GetCurrrentClient is a handy method, you can optionally include this    
                           //in your callbacks just to let your clients know who exactly sent the publication  
                            _subscriber.Key.PriceChangeCallback(e.Item, e.Price, e.Change, GetCurrentClient());  
                    }  
                 }  
             }  
    

    or distinguish your clients based on their usernames, which you should ideally have in your databse as well:

     lock (subscribers)  
                {  
                    foreach (var _subscriber in subscribers)  
                    {  
                        if(_subscriber.Value == "Jimmy86"))  
                        {  
                            //Identify a specific client by their username and don't send the notification to him  
                            //here we send the notification to everyone but jimmy86  
                            continue;  
                        }  
                        else  
                        {  
                            _subscriber.Key.PriceChangeCallback(e.Item, e.Price, e.Change, GetCurrentClient());  
                        }  
                    }  
                }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.