SignalR。计时器没有在服务器上停止

时间:2016-06-03 09:50:33

标签: signalr

我们正在使用SignalR进行信息交换。

连接Web浏览器时会启动计时器,但在用户关闭浏览器时它不会停止。 这是代码。浏览器连接时会运行starttimer函数。 当用户断开浏览器时,计时器仍在服务器上运行。

[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)
        };
    }

}

1 个答案:

答案 0 :(得分:2)

除了它注释掉的事实?你是否在计时器上设置了一个断点,看它是否会被击中?可能是在调用onDisconnect时存在延迟,如果超时属性设置得太大,则可能不会触发。如果它不知道客户端已关闭,它可能会进入onReconnected。