Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a specific client from WebSocketCollection

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.

like image 845
parveenkhtkr Avatar asked Jul 30 '13 19:07

parveenkhtkr


1 Answers

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.

like image 125
parveenkhtkr Avatar answered Sep 20 '22 01:09

parveenkhtkr