从会话中获取行为实例

本文关键字:实例 获取 会话 | 更新日期: 2023-09-27 17:49:20

如果我理解正确的话,每次有连接请求时,都会初始化一个Behavior实例。所以我将一些信息保存到扩展的Behavior类中。但是,现在我想发送消息给一些连接的客户端(例如,具有属性productBehavior.Type == ProductType.Main的行为实例)。

我怎样才能做到这一点?

        foreach (var session in this.Sessions.Sessions)
        {
            // How to get the behavior instance here, so I can get the property value?
        }

从会话中获取行为实例

sessionIWebSocketSession, Behavior也实现该接口。所以foreach中的session变量实际上是Behavior。我们只需要将它强制转换为Behavior:

    foreach (var session in this.Sessions.Sessions)
    {
        MyBehavior b = session as MyBehavior;
        // Do anything
    }