Signalr 信号员。服务器上的计时器未停止

Signalr 信号员。服务器上的计时器未停止,signalr,Signalr,我们使用信号机进行信息交换 连接web浏览器时,计时器将启动,但当用户关闭浏览器时,计时器不会停止。 这是代码开始定时器功能运行。 当用户断开浏览器连接时,计时器仍在服务器上运行 [HubName("myChatHub")] public class InboundCallsDataShare : Hub { private OverrideTimer timer ; private List<GroupNConnectionId> groupsList = new

我们使用信号机进行信息交换

连接web浏览器时,计时器将启动,但当用户关闭浏览器时,计时器不会停止。 这是代码<连接浏览器时,代码>开始定时器功能运行。 当用户断开浏览器连接时,计时器仍在服务器上运行

[HubName("myChatHub")]
public class InboundCallsDataShare : Hub
{
    private OverrideTimer timer ;
    private List<GroupNConnectionId> groupsList = new List<GroupNConnectionId>();
    public void send(string message)
    {
        Clients.All.addMessage(message);
        //Clients..addMessage(message);

    }

    public void starttimer(string queue)
    {
        //var connectionId = this.Context.ConnectionId;
        //GroupNConnectionId objGroupNConnectionId=new GroupNConnectionId();
        //objGroupNConnectionId.Group = queue;
        //objGroupNConnectionId.ConnectionID = connectionId;
        //if(groupsList.Contains(objGroupNConnectionId))return;
        //////////////////////////////////////////////////////
        //groupsList.Add(objGroupNConnectionId);
        Groups.Add(this.Context.ConnectionId, queue);
        timer = new OverrideTimer(queue);
        timer.Interval = 15000;
        timer.Elapsed +=new EventHandler<BtElapsedEventArgs>(timer_Elapsed);
        //first time call
        timer_Elapsed(timer,new BtElapsedEventArgs(){Queue = queue});
        //ends
        timer.Start();
        Console.WriteLine("Timer for queue " +queue);
    }

    public override Task OnConnected()
    {
       return base.OnConnected();
    }
    public override Task OnDisconnected()
    {

        //timer.Stop();
        return base.OnDisconnected();
    }



    public void getdatafromxml(string queue)
    {

        string list = (new Random()).Next(1, 10000).ToString();
        Clients.All.getList(list);

        //Clients..addMessage(message);

    }
    public ICBMObject GetInterationList(string queue)
    {
        //ININInterations.QueueListViewItemData _obj = new ININInterations.QueueListViewItemData();
        return GetInboundCallCountFromXML(queue);
        //return _obj.MainFunctionIB();

    }

    void timer_Elapsed(object sender, BtElapsedEventArgs e)
    {

        ICBMObject objICBMObject = GetInboundCallCountFromXML(e.Queue);
        Clients.Group(e.Queue).getList(objICBMObject);
        CreateFile(e.Queue);
        //Clients.All.getList(objICBMObject);
    }

    private void CreateFile(string queue)
    {
        string path = @"D:\t.txt";
        string text = File.ReadAllText(path);
        text += queue+ DateTime.Now.ToString() + Environment.NewLine;
        File.WriteAllText(path, text);
    }
    public ICBMObject GetInboundCallCountFromXML(string queue)
    {
        FileStream fs = null;
        int totalInboundCalls = 0,
                totalInboundCallsUnassigned = 0;
        string longestDuration = "";
        bool updateText = false;
        try
        {
            XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNodeList xmlnode;
            int i = 0;
            string str = null;
            fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "InboundXML/" + queue + ".xml",
                FileMode.Open, FileAccess.Read);
            if (fs.CanRead)
            {
                xmldoc.Load(fs);
                xmlnode = xmldoc.GetElementsByTagName(queue);

                for (i = 0; i <= xmlnode.Count - 1; i++)
                {

                    totalInboundCalls = Convert.ToInt32(xmlnode[i].ChildNodes.Item(0).InnerText.Trim());
                    totalInboundCallsUnassigned = Convert.ToInt32(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
                    longestDuration = xmlnode[i].ChildNodes.Item(2).InnerText.Trim();

                }
                updateText = true;
            }
        }
        catch (Exception)
        {


        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
                fs.Dispose();
            }
        }


        return new ICBMObject()
        {
            TotalInboundCalls = totalInboundCalls,
            TotalInboundCallsUnassigned = totalInboundCallsUnassigned,
            LongestDuration = longestDuration,
            UpdateText = updateText
            //string.Format("{0:D2}:{1:D2}:{2:D2}",
            //    _LongetInbound.Hours,
            //    _LongetInbound.Minutes,
            //    _LongetInbound.Seconds)
        };
    }

}
[HubName(“myChatHub”)]
公共类InboundCallsDataShare:Hub
{
专用覆盖定时器;
私有列表组列表=新列表();
公共无效发送(字符串消息)
{
Clients.All.addMessage(message);
//客户端..添加消息(消息);
}
公共void starttimer(字符串队列)
{
//var connectionId=this.Context.connectionId;
//GroupNConnectionId objGroupNConnectionId=新的GroupNConnectionId();
//objGroupNConnectionId.Group=队列;
//objgroupnconconnectionId.ConnectionID=ConnectionID;
//if(groupsList.Contains(objgroupnconconnectionid))返回;
//////////////////////////////////////////////////////
//groupsList.Add(objgroupnconconnectionid);
添加(this.Context.ConnectionId,队列);
计时器=新的覆盖器(队列);
定时器间隔=15000;
timer.appeased+=新事件处理程序(timer\u appeased);
//第一次电话
计时器(计时器,新的BtElapsedEventArgs(){Queue=Queue});
//结束
timer.Start();
Console.WriteLine(“队列计时器”+队列);
}
已连接的公用覆盖任务()
{
返回base.OnConnected();
}
公共覆盖任务OnDisconnected()
{
//timer.Stop();
返回base.OnDisconnected();
}
public void getdatafromxml(字符串队列)
{
stringlist=(newrandom()).Next(11000).ToString();
Clients.All.getList(列表);
//客户端..添加消息(消息);
}
公共ICBMObject GetInteractionList(字符串队列)
{
//InInInteractions.QueueListViewItemData_obj=新建InInInInteractions.QueueListViewItemData();
返回GetInboundCallCountFromXML(队列);
//返回_obj.MainFunctionIB();
}
无效计时器已过(对象发送器,BtElapsedEventArgs e)
{
ICBMObject objICBMObject=GetInboundCallCountFromXML(e.Queue);
Clients.Group(e.Queue).getList(objicbm对象);
CreateFile(e.Queue);
//Clients.All.getList(对象);
}
私有void创建文件(字符串队列)
{
字符串路径=@“D:\t.txt”;
string text=File.ReadAllText(路径);
text+=queue+DateTime.Now.ToString()+Environment.NewLine;
File.WriteAllText(路径,文本);
}
公共ICBMObject GetInboundCallCountFromXML(字符串队列)
{
FileStream fs=null;
int totalInboundCalls=0,
totalInboundCallsUnassigned=0;
字符串最长持续时间=”;
bool updateText=false;
尝试
{
XmlDataDocument xmldoc=新的XmlDataDocument();
XmlNodeList xmlnode;
int i=0;
字符串str=null;
fs=新文件流(AppDomain.CurrentDomain.BaseDirectory+“InboundXML/”+队列+“.xml”,
FileMode.Open,FileAccess.Read);
if(fs.CanRead)
{
xmldoc.Load(fs);
xmlnode=xmldoc.GetElementsByTagName(队列);

对于(i=0;i除了它被注释掉的事实之外?您是否在计时器上设置了一个断点以查看它是否被命中?可能是调用onDisconnect时有延迟,如果timeout属性设置得太大,它可能不会启动。如果它不知道客户端已关闭,它可能正在输入onReconnected。

CreateFile(e.Queue);在15秒后执行,这不是必需的..当检测到窗口或选项卡关闭时,是否在客户端调用disconnect?否,我没有在客户端调用disconnect上的任何函数。但是服务器端我们有公共覆盖任务OnDisconnected(){if(timer!=null)timer.Dispose();返回base.OnDisconnected();}如果希望启动该方法,或等待预配置的连接超时时间,则需要在客户端调用disconnect。读取。虽然有点长,但绝对是必须读取的。此处描述的事件序列不保证。Signal会尽一切努力以可预测的方式根据对于此方案,网络事件和底层通信框架(如传输API)处理网络事件的方式有很多种。例如,当客户端重新连接时,可能不会引发重新连接的事件,或者当尝试建立连接失败时,服务器上的OnConnected处理程序可能会运行这描述了某些典型环境通常会产生的影响,仅供参考