下面是我的WebSocket结构。
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();
}
}
如下,我可以发送给大家。
Clients.Broadcast(message);
但是,当我想逐一发送时:
foreach (var item in Clients)
{
var UserModel = item.Kullanici;
}
错误信息:
'WebSocketHandler' does not contain a definition for 'Kullanici' and no extension method 'Kullanici' accepting a first argument of type 'WebSocketHandler' could be found.
发布于 2018-06-04 19:09:42
我认为你应该能够先把它转换成继承的类。
var UserModel = ((SocketHandler)item).Kullanici
https://stackoverflow.com/questions/-100008625
复制相似问题