2015-10-13 32 views
3

asp.net的MVC頁的問題是不是this重複的整合,雖然提到的問題有一些非常有趣的答案和幫助我瞭解我的問題更好,但它不符合我的需要。讓我解釋一下我的情況。如何含有signalR與其他平臺

我有一個asp.net mvc的網站,具有signalR & SQL依賴通知功能和實時數據更新。用戶身份驗證通過使用Identity 2.0完成。 只有授權用戶才能看到更新的數據/通知。此外,通知/更新使用戶不同。我通過實現自定義UserId提供程序並使用Identity的UserId來實現此目的。

現在我要實現以下目標。

  • 簡單部分:在其他網站中顯示頁面(儀表板)的內容,而不管其開發語言如何。它可以注入現有頁面或他們(其他網站)想要的任何地方。

  • 困難部分:根據登錄用戶顯示實時通知&集成部分的更新。

什麼是在這種情況下執行的最佳行動方案?

更新 由於原來的問題被擱置,由於沒有指定的細節,所以這裏有關於這一點我想獲得建議的細節。

我有一個asp.net mvc的網站運行在本地主機:54603,以下是家居控制器

public ActionResult Index() 
{ 
    HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); 
    return View();    
} 

索引視圖上有一些signalR功能的作用。下面是signalR的啓動配置,以允許CORS因爲我我的目標是揭露索引頁面及其全功能客戶端(運行PHP等)

app.Map("/signalr", map => 
{ 

    map.UseCors(CorsOptions.AllowAll); 
    var hubConfiguration = new HubConfiguration 
    { 
     // You can enable JSONP by uncommenting line below. 
     // JSONP requests are insecure but some older browsers (and some 
     // versions of IE) require JSONP to work cross domain 
     // EnableJSONP = true 
    }; 

    map.RunSignalR(hubConfiguration); 
}); 

我創建notifcationHub和下面是我在前面的代碼結束(使用代理)。

<script> 
    $(function() { 
     // Reference the auto-generated proxy for the hub. 
     $.connection.notificationHub.url = 'http://localhost:54603/signalr'; 
     var notification = $.connection.notificationHub; 

     // Client side method for receiving the list of notifications on the connected event from the server 
     notification.client.refreshNotification = function (data) { 
      $("#notificationTab").empty(); 
      $("#cntNotifications").text(data.length); 
      for (var i = 0; i < data.length; i++) { 
       $("#notificationTab").append("<tr> <td> " + data[i].Id + "</td> <td>" + data[i].Text + "</td> <td>" + data[i].CreatedDate + "</td></tr>"); 
      } 
     } 

     //Client side method which will be invoked from the Global.asax.cs file. 
     notification.client.addLatestNotification = function (data) {  
      $("#cntNotifications").text($("#cntNotifications").text() + 1); 
      $("#notificationTab").append("<tr> <td> " + data.Id + "</td> <td>" + data.Text + "</td> <td>" + data.CreatedDate + "</td></tr>"); 
     } 

     // Start the connection. 
     $.connection.hub.start().done(function() { 

      //When the send button is clicked get the text and user name and send it to server. 
      $("#btnSend").click(function() { 
       notification.server.sendNotification($("#text").val(), $("#userName").val()); 
      }); 
      console.log($.connection.hub.id); 

     }).fail(function (data) { console.log('Could not connect' + data); }); 

    }); 
</script> 

和HTML是以下

@{ 
    ViewBag.Title = "Home Page"; 
    Layout = null; 
} 

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" /> 
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script> 

<script src="@Url.Content("~/signalr/hubs")"></script> 

@*<script src="~/signalr/hubs"></script>*@ 

<div style="width: 70%; padding: 20px"> 
    <div class="panel panel-primary"> 
     <div class="panel-heading"> 

      <! &ndash; To show notification count--> 
      <div style="float: left" class="panel-title">Notifications</div> 
      <div style="float: right" class="badge" id="cntNotifications"></div> 
      <div style="clear: both"></div> 


     </div> 
     <div class="panel-body"> 
      <! &ndash; To show All the notifications--> 
      <table class="table table-striped table-hover "> 
       <thead> 
        <tr> 
         <th>#</th> 
         <th>Text</th> 
         <th>Created Date</th> 
        </tr> 
       </thead> 

       <tbody id="notificationTab"></tbody> 
      </table> 
     </div> 
    </div> 

    <! &ndash; Add panel notification to send notification, Make sure that user enters the user id of the domain they are logged into --> 
    <div class="panel panel-primary"> 
     <div class="panel-heading"> 
      <h3 class="panel-title">Create Notifications</h3> 
     </div> 
     <div class="panel-body"> 
      <div class="form-group"> 
       <label class="control-label" for="focusedInput">Notification Text</label> 
       <input class="form-control" id="text" type="text" value=""> 
      </div> 
      <div class="form-group"> 
       <label class="control-label" for="focusedInput">Send To</label> 
       <input class="form-control" id="userName" type="text" value=""> 
      </div> 
      <a id="btnSend" style="cursor: pointer" class="btn btn-primary">Send Notification</a> 
     </div> 
    </div> 
</div> 

頁運行良好預期和連接ID在控制檯被記錄。 enter image description here

現在,我已經創建了另一個HTML頁中,下面是它的代碼

<html> 
<head> 
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" /> 
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> 
    <script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script> 
</head> 
<body> 
    <aside> 
     <div class="col-lg-2"> 
       <ul> 
        <li>this</li> 
        <li>is</li> 
        <li>aside</li> 
       </ul> 
       </div> 
    </aside> 
    <article> 
     <div class="col-lg-10"> 
      <div id="something"> 

      </div> 
     </div> 
    </article> 
    <script src="http://localhost:54603/signalr/hubs"></script> 
    <script> 
     $(document).ready(function() { 
      $.ajax({ 
       url: "http://localhost:54603/Home/Index", 
       dataType: 'html', 
       crossDomain: true, 
       cache:true 


      }).done(function (data) { 
       $('#something').html(data); 
      }); 
     }); 
    </script> 
</body> 
</html> 

的Ajax請求返回HTML,但signalR拋出錯誤。以下是控制檯消息。

GET http://localhost:54603/Home/Index 200 OK 1.18s
Could not connect Error: Error during negotiation request.

我在做什麼錯誤導致了這個錯誤?

+1

我個人覺得這個問題太寬泛。太多的語言,太多的可能的解決方案,太多的問題:(如果你削減了一個特定的東西,你到目前爲止嘗試過的東西以及你想要克服的倒下的東西會更好,你可以使用SignalR [cross domain](http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client),這是否有幫助? – Luke

+0

我同意和當然最好的事情會想出一些通用的,但對於初學者來說,讓我們假設客戶端是PHP將在哪裏集成。有什麼建議? – rollo

+2

好的SignalR跨域在客戶端使用JavaScript,在服務器端使用CORS,所以你'您可以輕鬆創建一個JavaScript文件,該文件可以連接到SignalR頻道,而不需要對PHP代碼進行任何更改,只需將JavaScript包含在客戶端的頁面中即可。 – Luke

回答

1

這很難牽制你需要做的,造成的問題是相當廣泛的到底是什麼,但我希望這點幫助(ED)有點:

SignalR cross-domain使用JavaScript在客戶端和CORS的服務器端,所以你被覆蓋在前面。除了將JavaScript包含在客戶端頁面中之外,您可以輕鬆創建一個JavaScript文件,該文件可以連接到SignalR頻道,而無需更改PHP代碼。

您可以很容易地在您的服務器上創建一個腳本,客戶端可以在其頁面上引用該腳本,將所需的所有內容都拉到客戶端頁面上。由於該腳本是從您自己的服務器執行的,我不認爲您需要擔心CORS,因爲它不是真正的「跨站點腳本」,因爲執行請求的代碼將由您的服務器/域「擁有」。

至於身份驗證,如果只有您的腳本需要訪問身份驗證,我認爲這意味着您也可以使用您的域的身份驗證(餅乾等),但我不得不檢查那個。最終,您將使用HTTPs請求從包含的腳本對您的域進行身份驗證?

以下是一些有用的線程可能與一個幫助: