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 ?