4

Controller類凡集線器的定義客戶端功能沒有得到所謂的SignalR

public abstract class MonitoringProfileLogChartController : Croem.NotificationManager.Website.Base.BaseController.BaseController 
     { 

      public ActionResult Index() 
      { 
       BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetRegisteredContexts(); 
       return base.TransalateToAction(result); 
      } 
      public ActionResult LiveMonitoringProfileLogChart() 
      { 
       return View(); 
      } 
      public ActionResult test() 
      { 
       return View(); 
      } 

**below is rest of the code of controller where our focus should be** 


    public JsonResult GetMonitoringProfileLogChart(string FromDate, string ToDate, int ContextId) 
     { 
      BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetMonitoringProfileLogChart(FromDate, ToDate, ContextId); 
      return Json(result.Model, JsonRequestBehavior.AllowGet); 
     } 
     public JsonResult GetMonitoringProfileLiveLogChart(string FromTime, string ToTime, string DataMinutes) 
     { 
      BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetMonitoringProfileLiveLogChart(FromTime, ToTime, DataMinutes); 
      IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 
      context.Clients.All.addMessage(result.Model); 
      var hub = new MyHub(); 
      hub.Send("", ""); 
      return Json(result.Model, JsonRequestBehavior.AllowGet); 

     } 
     public JsonResult GetMonitoringProfileCombinationChart(string FromTime, string ToTime) 
     { 
      BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetMonitoringProfileCombinationChart(FromTime, ToTime); 
      return Json(result.Model, JsonRequestBehavior.AllowGet); 
     } 
    } 
    public class MyHub : Hub 
    { 
     IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 
     public void Send(string name, string message) 
     { 

      BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetMonitoringProfileLiveLogChart(null, null, null); 
      context.Clients.All.addMessage(result.Model); 
     } 
     public void test() 
     { 

      BusinessLogicReturn result = new ProcessBusinessLogic.Logic().GetMonitoringProfileLiveLogChart(null, null, null); 
      context.Clients.All.addMessage(result.Model); 

     } 

    } 

它用於映射服務器URL

class Program 
{ 
    static void Main(string[] args) 
    { 
     string info = LoggingServer.Open(); 
     Console.WriteLine(info); 

     string url = "http://localhost:8080"; 
     using (WebApp.Start<Startup>(url)) 
     { 
      Console.WriteLine("Server running on {0}", url); 
      var hubs = new Croem.NotificationManager.Website.Base.Controllers.MyHub(); 
      Console.ReadLine(); 
     } 
     LoggingServer.Close(); 
    } 
    class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // Turn cross domain on 
      var config = new HubConfiguration { EnableCrossDomain = true }; 

      // This will map out to http://localhost:8080/signalr by default 
      app.MapHubs(config); 
     } 
    } 

} 

客戶端頁面

<!DOCTYPE html> 
<html> 
<head> 
    <title>SignalR Live Chat</title> 

</head> 
<body> 
    <div class="container"> 
     <input type="text" id="message" /> 
     <input type="button" id="sendmessage" value="Send" /> 
     <input type="hidden" id="displayname" /> 
     <strong>Error Count</strong> 
     <input type="text" id="Error_count" value="0" /> 
     <ul id="discussion"></ul> 
    </div> 
    <!--Script references. --> 
    <!--Reference the jQuery library. --> 
    <script src="Scripts/jquery-1.7.1.min.js"></script> 
    <!--Reference the SignalR library. --> 
    <script src="Scripts/jquery.signalR-1.1.3.js"></script> 
    <!--Reference the autogenerated SignalR hub script. --> 
    <script src="http://localhost:8080/signalr/hubs"></script> 
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript"> 
     var chart; 
     var timer; 
     $(function() { 



       Highcharts.setOptions({ 
        global: { 
         useUTC: false 
        } 
       }); 
       //Set the hubs URL for the connection 
       $.connection.hub.url = "http://localhost:8080/signalr"; 

       // Declare a proxy to reference the hub. 
       var chat = $.connection.myHub; 

       // Create a function that the hub can call to broadcast messages. 
       chat.client.addMessage = function (data) { 

        dataRecieved(data); 
        clearInterval(timer); 
        timer = setInterval(function() { 
         dataNotRecieved(); 
        }, 10000); 
       }; 
       $.connection.hub.logging = true; 
       $.connection.hub.start().done(function() { 
        $('#sendmessage').click(function() { 
         // Call the Send method on the hub. 
         chat.server.send($('#displayname').val(), $('#message').val()); 
         // Clear text box and reset focus for next comment. 
         $('#message').val('').focus(); 
        }); 
       }); 

       $.ajax({ 
        type: "GET", 
        dataType: "json", 
        data: { 
         DataMinutes: 5, 
         FromTime: null, 
         ToTime: null 
        }, 
        url: "@Url.Action("GetMonitoringProfileLiveLogChart", "MonitoringProfileLogChart")", 
        success: function (data) { 
         chart = new Highcharts.Chart({ 
          chart: { 
           renderTo: 'container', 
           type: 'spline', 
           animation: Highcharts.svg, // don't animate in old IE 
           marginRight: 10, 


          }, 
          title: { 
           text: 'Live Profile Monitoring' 
          }, 
          xAxis: { 
           type: 'datetime', 

           tickPixelInterval: 150 
          }, 
          yAxis: { 
           title: { 
            text: 'Value' 
           }, 
           plotLines: [{ 
            value: 0, 
            width: 1, 
            color: '#808080' 
           }] 
          }, 
          tooltip: { 
           formatter: function() { 
            return '<b>' + this.series.name + '</b><br/>' + 
            Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + 
            Highcharts.numberFormat(this.y, 2); 
           } 
          }, 
          legend: { 
           enabled: true 
          }, 
          exporting: { 
           enabled: false 
          }, 
          series: data.series, 

         }); 
        } 
       }); 


       timer = setInterval(function() { 
        dataNotRecieved(); 
       }, 10000); 

       function dataNotRecieved() { 
        var shift = false; 
        for (var j = 0; j < chart.series.length; j++) { 
         if (chart.series[j].data.length < 50) { 
          shift = false; 

         } 
         else { 
          shift = true; 
         } 
         chart.series[j].addPoint([new Date().getTime() - 4 * 1000 * 60 * 60, 0], true, shift); 
        } 

       } 
       function dataRecieved(data) { 
        // checking if series exsist in chart but is not in data coming from ajax call . and if it does not exsist add that series point with zero 
        var series_name_exist = 0; 
        var series_exist = 0; 
        var index = 0; 
        var shift = false; 
        var length = chart.series.length; 
        for (var j = 0; j < chart.series.length; j++) { 

         for (var k = 0; k < data.series.length; k++) { 
          if (chart.series[j].name == data.series[k].name) { 
           series_name_exist = 1; 
           break; 
          } 

         } 
         if (series_name_exist == 0) { 

          if (chart.series[j].data.length < 50) { 
           shift = false; 

          } 
          else { 
           shift = true; 
          } 
          chart.series[j].addPoint([new Date().getTime() - 4 * 1000 * 60 * 60, 0], true, shift); 
         } 
         else { 
          series_name_exist = 0; 
         } 



        } 
        // if series exist add point otherwise add series 
        for (var k = 0; k < data.series.length; k++) { 

         for (var j = 0; j < chart.series.length; j++) { 
          if (chart.series[j].name == data.series[k].name) { 
           series_exist = 1; 
           index = j; 
           break; 
          } 

         } 
         if (series_exist == 1) { 

          if (chart.series[index].data.length < 50) { 
           shift = false; 

          } 
          else { 
           shift = true; 
          } 
          //chart.series[index].addPoint([data.series[k].time, data.series[k].count], true, shift); 
          chart.series[index].addPoint([new Date().getTime() - 4 * 1000 * 60 * 60, data.series[k].count], true, shift); 
          series_exist = 0; 

         } 

         else { 

          chart.addSeries({ name: '' + data.series[k].name + '', data: [] }); 
          //chart.series[length].addPoint([data.series[k].time, data.series[k].count], true); 
          chart.series[length].addPoint([new Date().getTime() - 4 * 1000 * 60 * 60, data.series[k].count], true); 
          length = length + 1; 
         } 


        } 
       } 



       $('#message').focus(); 
       // Start the connection. 


     }); 

    </script> 
    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

</body> 
</html> 
控制檯應用程序 個

實際屏幕 screen 第一選定類是被用於映射服務器URL 第二選定的類是其中轂被定義 directories

我面臨一個問題,當我提出的控制器類的控制檯應用程序類調用MyHub的對象並從控制器調用該函數被調用,但另一方面,我的HTML頁面上沒有顯示任何內容,當我按屏幕上的發送按鈕時,它調用相同的功能,但輸出也顯示在頁面上。請告訴我爲什麼會發生這種情況,以及如何從控制器調用函數,以便輸出顯示在HTML頁面上。我想我沒有指定它應該寫的中心URL,這就是爲什麼它不直接調用客戶端功能,而是直接從控制器類發送它,而不是從客戶端調用中心功能,因爲在客戶端聊天URL被指定,我認爲這就是爲什麼當我從客戶端頁面客戶端調用發送功能添加消息執行。我無法找到指定集線器地址的方法

當我按屏幕上的發送按鈕時,顯示以下日誌消息:SignalR:觸發集線器'MyHub'上的客戶端集線器事件'addMessage'。「。

但是,當我在「GetMonitoringProfileLiveLogChart」函數中直接從控制器調用它時,此日誌消息未顯示。

SignalR版本1.1.3

回答

12

對我來說,它看起來像什麼應該在網頁上顯示,這是由於沒有開始之前集線器開往集線器的客戶端功能。

因此,要解決您的問題,只是增加你的「方法addMessage」功能,客戶中心,你做$.connection.hub.start前:

chat.client.addMessage = function() {...} 

$.connection.hub.start().done(function() {...}); 

接下來,您必須在代碼中的幾個誤區:

  1. 你」基本上做document.ready兩次,做$(function() {....})本質上是document.ready,但是你也在裏面做$(document).ready(function() {...})
  2. 在您的集線器中,您通過集線器上下文發送給客戶端。這有效,但沒有必要這樣做。你的方法不是靜態的,你應該只能做Clients.All.addMessage(...)
+0

馬倫它仍然沒有工作 – user2137186

+0

它的正常工作對我來說,發佈您的代碼 –

+0

您可以從服務器直接推送消息到客戶端的其餘部分,而不從客戶端調用它? – user2137186