Asp.Net Mvc WebSocket - 客户端的自定义参数
Asp.Net Mvc WebSocket - Custom Arguments for Clients
我的 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.
item.Kullanici => 正如您在 picture
中看到的
我认为您应该能够首先将其转换为继承的 class,例如
var UserModel = ((SocketHandler)item).Kullanici
你没有显示,但我假设 Clients
中的项目是 WebSocketHandler
类型
我的 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.
item.Kullanici => 正如您在 picture
中看到的我认为您应该能够首先将其转换为继承的 class,例如
var UserModel = ((SocketHandler)item).Kullanici
你没有显示,但我假设 Clients
中的项目是 WebSocketHandler