SignalR:不调用集线器中的方法来实时返回JSON

时间:2012-09-02 09:42:56

标签: c# asp.net-mvc json asp.net-web-api signalr

我正在使用SignalR运行性能监控过程时实时返回JSON(尽管它不起作用)。目前,我可以在完成流程后获得完整的JSON,但是我想实现它,以便当应用程序从程序计数器获取值时,它每秒返回JSON。问题是,我的Invoke调用根本就没有做任何事情,即使我在从性能监视中检索到的每个值中明确地返回要发送到浏览器的数据。

我使用ASP.NET MVC 4 Web API开发API,并在浏览器中通过URL调用API并返回JSON。我试图使用SignalR让应用程序将JSON发送到浏览器,但它根本没有做任何事情。相反,应用程序只需在进程完成后从控制器操作返回已完成的JSON以及所有性能数据值。换句话说,SignalR无效。

所以,我需要一些帮助来弄清楚为什么SignalR没有向浏览器发送任何数据,所以我可以获得实时反馈并结束这个项目。

请注意:由于我将JSON直接发送到浏览器,因此调用此操作是在模型类而非视图中完成的,因为当您通过返回JSON时无法使用视图对Web API的URL调用。请记住,我不是在使用视图,而是使用浏览器和对API的URL调用。

非常感谢任何指向正确方向的帮助,谢谢。如果我问一个愚蠢的问题,我很抱歉,但我一直在努力解决这个问题而根本无法找到解决方案。

这是我的集线器课程。它位于〜/ signalr中,位于名为LiveHub.cs的文件中。方法Send是我试图在下一个代码块中看到的方法调用的。

namespace PerfMon2.signalr
{
    public class LiveHub : Hub
    {

        public List<DataValueInfo> Send(List<DataValueInfo> data)
        {
            return data;
        }
    }
}

以下是LogDBRepository.cs中包含SignalR调用的方法。

public List<LogInfo> LogTimedPerfData(string macName, string categoryName, string counterName,
                                          string instanceName, string logName, string live, long? seconds)
    {
        iModsDBRepository modsDB = new iModsDBRepository();
        List<MachineInfo> theMac = modsDB.GetMachineByName(macName);

        if (theMac.Count == 0)
            return new List<LogInfo>();

        else if (instanceName == null)
        {
            if (!PerformanceCounterCategory.Exists(categoryName, macName) ||
                !PerformanceCounterCategory.CounterExists(counterName, categoryName, macName) )
            {
                return new List<LogInfo>();
            }
        }
        else if (instanceName != null)
        {
            if (!PerformanceCounterCategory.Exists(categoryName, macName) ||
                !PerformanceCounterCategory.CounterExists(counterName, categoryName, macName) ||
                !PerformanceCounterCategory.InstanceExists(instanceName, categoryName, macName))
            {
                return new List<LogInfo>();
            }
        }
        else if (logName == null)
        {
            return new List<LogInfo>();
        }

        // Check if entered log name is a duplicate for the authenticated user
        List<LogInfo> checkDuplicateLog = this.GetSingleLog(logName);
        if (checkDuplicateLog.Count > 0)
        {
            return new List<LogInfo>();
        }

        PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, theMac[0].MachineName);
        if (category.CategoryName == null || category.MachineName == null)
        {
            return new List<LogInfo>();
        }

        List<LogInfo> logIt = new List<LogInfo>();
        if (category.CategoryType != PerformanceCounterCategoryType.SingleInstance)
        {
            List<InstanceInfo> instances = modsDB.GetInstancesFromCatMacName(theMac[0].MachineName, category.CategoryName);

            foreach (InstanceInfo inst in instances)
            {
                if (!category.InstanceExists(inst.InstanceName))
                {
                    continue;
                }
                else if (inst.InstanceName.Equals(instanceName, StringComparison.OrdinalIgnoreCase))
                {
                    PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName,
                                                                        inst.InstanceName, theMac[0].MachineName);

                    //CounterSample data = perfCounter.NextSample();
                    //double value = CounterSample.Calculate(data, perfCounter.NextSample());
                    string data = "";
                    List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name);

                    string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");

                    List<string> dataValues = new List<string>();

                    var hubConnection = new HubConnection("http://localhost/PerfMon2");
                    hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
                    var perfMon = hubConnection.CreateProxy("LiveHub");
                    // perfMon.On("sendValue", message => Console.WriteLine(message));
                    hubConnection.Start().Wait();

                    List<DataValueInfo> lol = new List<DataValueInfo>();
                    for (int i = 0; i < seconds; i++)
                    {
                        data = "Value " + i + ": " + perfCounter.NextValue().ToString();
                        //dataValues[i] = data;
                        dataValues.Add(data);
                        lol.Add(new DataValueInfo
                        {
                            Value = perfCounter.NextValue().ToString()
                        });
                        perfMon.Invoke<List<DataValueInfo>>("Send", lol);
                        Thread.Sleep(1000);
                    }
                    string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");

                    Log log = new Log
                    {
                        LogName = logName,
                        CounterName = perfCounter.CounterName,
                        InstanceName = perfCounter.InstanceName,
                        CategoryName = perfCounter.CategoryName,
                        MachineName = perfCounter.MachineName,
                        TimeStarted = timeStarted,
                        TimeFinished = timeFinished,
                        PerformanceData = string.Join(",", dataValues),
                        UserID = currUser[0].UserID
                    };
                    this.CreateLog(log);
                    logIt.Add(new LogInfo
                    {
                        LogName = logName,
                        CounterName = perfCounter.CounterName,
                        InstanceName = perfCounter.InstanceName,
                        CategoryName = perfCounter.CategoryName,
                        MachineName = perfCounter.MachineName,
                        TimeStarted = timeStarted,
                        TimeFinished = timeFinished,
                        PerformanceData = dataValues.ToList<string>()
                    });
                    break;
                }
            }
        }
        else
        {
            PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName,
                                                                        "", theMac[0].MachineName);


            string data = "";
            List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name);

            string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");

            //string[] dataValues = new string[(int)seconds];
            List<string> dataValues = new List<string>();

            var hubConnection = new HubConnection("http://localhost/PerfMon2");
            hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
            var perfMon = hubConnection.CreateProxy("LiveHub");
            // perfMon.On("sendValue", message => Console.WriteLine(message));
            hubConnection.Start().Wait();

            List<DataValueInfo> lol = new List<DataValueInfo>();
            for (int i = 0; i < seconds; i++)
            {
                data = "Value " + i + ": " + perfCounter.NextValue().ToString();
                //dataValues[i] = data;
                dataValues.Add(data);
                lol.Add(new DataValueInfo
                {
                    Value = perfCounter.NextValue().ToString()
                });
                perfMon.Invoke<List<DataValueInfo>>("Send", lol);
                Thread.Sleep(1000);
            }
            string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");

            Log log = new Log
            {
                LogName = logName,
                CounterName = perfCounter.CounterName,
                InstanceName = perfCounter.InstanceName,
                CategoryName = perfCounter.CategoryName,
                MachineName = perfCounter.MachineName,
                TimeStarted = timeStarted,
                TimeFinished = timeFinished,
                PerformanceData = string.Join(",", dataValues),
                UserID = currUser[0].UserID
            };              
            this.CreateLog(log);
            logIt.Add(new LogInfo
            {
                LogName = logName,
                CounterName = perfCounter.CounterName,
                InstanceName = perfCounter.InstanceName,
                CategoryName = perfCounter.CategoryName,
                MachineName = perfCounter.MachineName,
                TimeStarted = timeStarted,
                TimeFinished = timeFinished,
                PerformanceData = dataValues.ToList<string>()
            });
        }
        return logIt;
    }

这是LogController.cs中方法的控制器:

  [AcceptVerbs("GET", "POST")]
    public List<LogInfo> Log_Perf_Data(string machine_name, string category_name, string counter_name, string instance_name,
                                   string log_name, long? seconds, string live, string enforceQuery)
    {
        LogController.CheckUser();

        // POST api/log/post_data?machine_name=&category_name=&counter_name=&instance_name=&log_name=&seconds=
        if (machine_name != null && category_name != null && counter_name != null && log_name != null && seconds.HasValue && enforceQuery == null)
        {
            List<LogInfo> dataVal = logDB.LogTimedPerfData(machine_name, category_name, counter_name, instance_name,
                                   log_name, live, seconds);
            logDB.SaveChanges();
            return dataVal;

        }

        return new List<LogInfo>();
    }

更新:

所以,我更新了hub方法,现在它已经

public void SendToClient(List<DataValueInfo> json)
{
    Clients.showValue(json);
}

在我的存储库方法中,我更新了它以便

var hubConnection = new HubConnection("http://localhost/PerfMon2");
hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
var perfMon = hubConnection.CreateProxy("LiveHub");                    //            
perfMon.On("showValue", json => Console.WriteLine(json));
hubConnection.Start().Wait();

List<DataValueInfo> lol = new List<DataValueInfo>();
for (int i = 0; i < seconds; i++)
{
    data = "Value " + i + ": " + perfCounter.NextValue().ToString();
    dataValues.Add(data);
    lol.Add(new DataValueInfo
    {
        Value = perfCounter.NextValue().ToString()
    });
    perfMon.Invoke<List<DataValueInfo>>("SendToClient", lol);
    Thread.Sleep(1000);
}

仍然没有运气。是因为我使用的是Console.WriteLine,它不能与浏览器一起使用?如果没有,我可以使用什么?如前所述,Javascript是不可能的,因为我没有使用View。

1 个答案:

答案 0 :(得分:1)

我认为您误解了一些SignalR基础知识,因为我看到您的中心没有呼叫Clients,这意味着您没有播放任何内容。

您的集线器上的公共方法(即您的Send()方法)现在非常无用,因为它只返回您传递给它的数据。如果要从服务器广播到连接的客户端,则需要在hub方法中使用Clients动态属性并在其上调用方法。

阅读这些文档可以让您更好地了解这应该如何运作:https://github.com/SignalR/SignalR/wiki/Hubs