I have connected two clients(.Net Console and flutter) in hub and trying to have conversation between them.
When I am sending message from flutter client to server the message is populated in console but when i am trying to send message from console client the message is not populating in flutter
public class NotificationHub : Hub
{
// I have used this line to store list of connected users
public static ConcurrentDictionary<string, MyUserType> MyUsers = new ConcurrentDictionary<string, MyUserType>();
public override Task OnConnected()
{
MyUsers.TryAdd(Context.ConnectionId, new MyUserType() { ConnectionId = Context.ConnectionId });
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
MyUserType garbage;
MyUsers.TryRemove(Context.ConnectionId, out garbage);
return base.OnDisconnected(stopCalled);
}
public void SendMessageToAllClients()
{
var listOfIds = MyUsers.Keys.ToList();
Clients.Client(listOfIds[0]).SendAsync("initsignal", "THis is message to server");
Clients.Client(listOfIds[1]).SendAsync("initsignal", "THis is message to server");
}
// This message is used to send message to clients
public void Send(string name, string message)
{
Debug.WriteLine(name + " " + message);
Clients.All.broadcastMessage(name, $"{message}");
Clients.Others.newMessage(new HubMes(name, message));
}
}
public class MyUserType
{
public string ConnectionId { get; set; }
// Can have whatever you want here
}
public class HubMes
{
public HubMes(string _name, string _mes)
{
name = _name;
message = _mes;
}
public string name { get; set; }
public string message { get; set; }
}
I have connected two clients(.Net Console and flutter) in hub and trying to have conversation between them. When I am sending message from flutter client to server the message is populated in console but when i am trying to send message from console client the message is not populating in flutter
Flutter Client code:
.Net Server code