【问题标题】:SignalR. Timer is not stopping on the server信号R。计时器未在服务器上停止
【发布时间】:2017-08-01 06:38:46
【问题描述】:

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

当网络浏览器连接时,计时器会启动,但当用户关闭浏览器时它不会停止。 这是代码。 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)
        };
    }

}

【问题讨论】:

  • CreateFile(e.Queue); 15 秒后执行,这不是必需的..
  • 当您检测到窗口或选项卡正在关闭时,您是否在客户端调用断开连接?
  • 不,我没有在与客户端断开连接时调用任何函数。但是服务器端我们有公共覆盖 Task OnDisconnected() { if(timer!=null) timer.Dispose();返回 base.OnDisconnected(); }
  • 如果要触发该方法或等待预配置的连接超时时间,则需要在客户端调用断开连接。阅读this。有点长,但绝对值得一读

标签: signalr


【解决方案1】:

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

【讨论】:

  • 此处描述的事件顺序无法保证。 SignalR 尽一切努力根据此方案以可预测的方式引发连接生命周期事件,但网络事件有许多变体,并且底层通信框架(如传输 API)处理它们的方式也很多。例如,当客户端重新连接时可能不会引发 Reconnected 事件,或者当尝试建立连接不成功时,服务器上的 OnConnected 处理程序可能会运行。这描述了某些典型情况通常会产生的影响
  • 仅供参考
猜你喜欢
  • 2019-05-12
  • 2020-04-12
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多