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">
<! – 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">
<! – 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>
<! – 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>
現在,我已經創建了另一個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.
我在做什麼錯誤導致了這個錯誤?
我個人覺得這個問題太寬泛。太多的語言,太多的可能的解決方案,太多的問題:(如果你削減了一個特定的東西,你到目前爲止嘗試過的東西以及你想要克服的倒下的東西會更好,你可以使用SignalR [cross domain](http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client),這是否有幫助? – Luke
我同意和當然最好的事情會想出一些通用的,但對於初學者來說,讓我們假設客戶端是PHP將在哪裏集成。有什麼建議? – rollo
好的SignalR跨域在客戶端使用JavaScript,在服務器端使用CORS,所以你'您可以輕鬆創建一個JavaScript文件,該文件可以連接到SignalR頻道,而不需要對PHP代碼進行任何更改,只需將JavaScript包含在客戶端的頁面中即可。 – Luke