Reputation: 1949
I am writing a websocket handler which should send a message from one client to another.
CODE
public class SocketHandler : WebSocketHandler
{
private static WebSocketCollection clients = new WebSocketCollection();
private int id;
public override void OnOpen()
{
this.id = Convert.ToInt32(Cypher.Decrypt(this.WebSocketContext.QueryString["id"]));
clients.Add(this);
}
public override void OnMessage(string message)
{
//sending code here
}
}
I know if I need to send a message to all connected clients I just need to do:
clients.Broadcast("message");
...but what I need is to send to a specific client with specific Id
assigned to it from query string - let's say 1156
.
How can I get the client with id=1156
from the clients collection?
I tried using lambda expressions but it's not working. It should be simple... I have done similar things before in LINQ but at this time I am totally lost.
Upvotes: 6
Views: 4927
Reputation: 1949
I finally managed to search through clients for a specific client and send the message specifically to him.
clients.SingleOrDefault(r => ((SocketHandler)r).id == 1156).Send("Hey 1156!");
You just need to do typecast
and then usual query works fine.
Upvotes: 11