1

My WebSocket structure is below.

public HttpResponseMessage Get(int id, string kod)
{
    if (HttpContext.Current.IsWebSocketRequest)
    {
        HttpContext.Current.AcceptWebSocketRequest(new SocketHandler(id, kod));      
        return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}

class SocketHandler : WebSocketHandler
{
    DnaEntities db = new DnaEntities();

    private static WebSocketCollection Clients = new WebSocketCollection();
    private Yonetim_Kullanici Kullanici;

    public SocketHandler(int KullaniciId, string OturumKontrolKod)
    {
        var sorgu = db.Yonetim_Kullanici.Where(k => k.Id == KullaniciId && k.OturumKontrolKod == OturumKontrolKod && k.Durum == 1);
        if (sorgu.Count() == 1)
        {
            Kullanici = sorgu.FirstOrDefault();
        }
    }

    public override void OnOpen()
    {
        if (Kullanici != null)
        {
            Clients.Add(this);
        }
        base.OnOpen();
    }
    public override void OnClose()
    {
        Clients.Remove(this);
        base.OnClose();
    }

    public override void OnMessage(string data)
    {
        foreach (var item in Clients)
        {
            var data = item.Kullanici
            //item.Send();
        }
    }

As follows, i can send to everyone.

Clients.Broadcast(message);

But, When I want to send one by one:

foreach (var item in Clients)
{
    var UserModel = item.Kullanici;
}

Error Message:

'WebSocketHandler' does not contain a definition for 'Kullanici' and no extension method 'Kullanici' accepting a first argument of type 'WebSocketHandler' could be found.

item.Kullanici => as you can see in the picture

Barış
  • 93
  • 8

1 Answers1

1

I think you should be able to cast it to the inherited class first, e.g.

var UserModel = ((SocketHandler)item).Kullanici

You haven't shown it, but I would assume that the items in Clients are of type WebSocketHandler

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thanks. Yes, cast take is good idea. @ADyson second question? – Barış May 31 '18 at 06:30
  • @BarışSamedHalıcı if you have a second question, please create a second separate question in a new post. I answered the original question as it was shown at the time. Since people get points for answering questions, you can't just keep on adding more questions to the same post forever. If my answer helped you with the original question, please mark as accepted. If you'd like my help with another question, please comment here with a link to it. Thanks. – ADyson May 31 '18 at 08:06
  • Yes, it's true. But the second question is the continuation of the first problem. The answer to the second question may vary according to the alternative of the first. Thanks. – Barış May 31 '18 at 08:29
  • If you still insist, i open a new question. No problem. – Barış May 31 '18 at 08:30
  • As far as I can see my answer is the only solution to the first problem, so there's not really any direct link between the two. You want to compare values in a List of Kisisel_Bildirimlerim objects to some values in a list of SocketHandler objects. How they came to exist (e.g. by casting or other methods) is not really relevant to be honest. And So yes please make a new question... but first do some research on comparing lists in C# in general, there may be a general example of the principle in existence already which you can adapt. – ADyson May 31 '18 at 08:41
  • Anyway, even if the second question does "follow on the first"...it's still a separate question about a different issue and a different piece of code. SO posts can be closed for being "too broad" if you ask multiple questions in one. – ADyson May 31 '18 at 08:44
  • Thank you for your interest. I will do as you say. Thanks. – Barış May 31 '18 at 09:28