我在mvc中使用信号r实现定时器应用程序。我已经从nuget安装了软件包Microsoft.AspNet.SignalR.2.0.3。但是我没有得到接口IDisconnect和iconconnected,就像直接信号r包中存在的那样。这里提供的任何替代解决方案,以便我可以编写这些接口的连接和断开连接的方法。我从链接中引用了这个教程:-http://www.dotnetcurry.com/showarticle.aspx?ID=826我已经创建了hub如下:-
public class TimerHub : Hub, Microsoft.AspNet.SignalR.IConnection
{
public Task Disconnect()
{
TimerEventHandler.Instance.Disconnect(Context.ConnectionId);
return Clients.Client(Context.ConnectionId).leave(Context.ConnectionId,
DateTime.Now.ToString());
}
public Task Connect()
{
string connectionName = Context.ConnectionId;
if (Context.User != null && Context.User.Identity != null
&& Context.User.Identity.IsAuthenticated)
{
TimerEventHandler.Instance.UpdateCache(
Context.User.Identity.Name,
Context.ConnectionId,
ConnectionStatus.Connected);
connectionName = Context.User.Identity.Name;
}
return Clients.Client(Context.ConnectionId).joined(connectionName,
DateTime.Now.ToString());
}
}
在SignalR 2.0.3中,OnConnected
和OnDisconnected
被定义为Hub
基类中的虚方法。
您不应该尝试实现IConnection
接口。
你的TimerHub
应该看起来像这样:
public class TimerHub : Hub
{
public override Task OnConnected()
{
TimerEventHandler.Instance.Disconnect(Context.ConnectionId);
return Clients.Caller.leave(Context.ConnectionId,
DateTime.Now.ToString());
}
public override Task OnDisconnected()
{
string connectionName = Context.ConnectionId;
if (Context.User != null && Context.User.Identity != null
&& Context.User.Identity.IsAuthenticated)
{
TimerEventHandler.Instance.UpdateCache(
Context.User.Identity.Name,
Context.ConnectionId,
ConnectionStatus.Connected);
connectionName = Context.User.Identity.Name;
}
return Clients.Caller.joined(connectionName,
DateTime.Now.ToString());
}
}