充分利用WebSocketCollection特定的客户端 [英] Getting a specific client from WebSocketCollection

查看:699
本文介绍了充分利用WebSocketCollection特定的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个WebSocket的处理程序应该从一个客户端发送消息到另一个。

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(信息);

...但我需要的是发送给特定编号从查询字符串分配给它一个特定的客户端 - 比方说 1156

...but what I need is to send to a specific client with specific Id assigned to it from query string - let's say 1156.

我怎样才能获得客户端 ID = 1156 来自客户端的集合?

How can I get the client with id=1156 from the clients collection?

我试着使用lambda前pressions,但它不工作。它应该是简单...我已经在LINQ但在这个时候,我完全失去了做类似的事情了。

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.

推荐答案

我终于设法通过客户为特定客户搜索和专门发送给他的信息。

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.

这篇关于充分利用WebSocketCollection特定的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆