【发布时间】:2016-01-11 04:56:38
【问题描述】:
这个问题不是this 的重复问题,虽然提到的问题有一些非常有趣的答案并帮助我更好地理解我的问题,但它并不能满足我的需要。让我解释一下我的情况。
我有一个 asp.net mvc 网站,具有通知功能和通过 signalR 和 SQL 依赖的实时数据更新。用户身份验证是使用 Identity 2.0 完成的。 只允许授权用户查看更新的数据/通知。此外,通知/更新因用户而异。我通过实现自定义 UserId 提供程序并使用 Identity 的 UserId 实现了这一点。
现在我想实现以下目标。
简单部分:在其他网站中显示页面(仪表板)的内容,无论其开发语言如何。它可以注入现有页面或他们(其他网站)想要的任何地方。
困难部分:根据登录用户在集成部分显示实时通知和更新。
在这种情况下执行的最佳行动方案是什么?
更新 由于原问题因未详细说明而被搁置,所以这里是我想获得建议的细节。
我有一个asp.net mvc网站在localhost:54603运行,下面是home控制器的动作
public ActionResult Index()
{
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
return View();
}
索引视图上有一些 signalR 功能。下面是允许 CORS 的 signalR 的启动配置,因为我的目标是向客户端公开索引页面及其全部功能(运行 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
无法连接错误:协商请求期间出错。
我做错了什么导致这个错误?
【问题讨论】:
-
个人觉得这个问题太笼统了。太多的语言,太多可能的解决方案,太多的问题:(。最好减少一件特定的事情,到目前为止你已经尝试过什么以及你想要克服的失败。你可以使用SignalR cross domain,有帮助吗?
-
我同意,当然最好的办法是想出一些通用的东西,但对于初学者来说,我们假设客户端是 php,它将被集成。对此有何建议?
-
那么 SignalR 跨域在客户端使用 JavaScript 并在服务器端使用 CORS,因此您已经覆盖了那里。您可以轻松创建一个 JavaScript 文件,该文件可以连接到 SignalR 通道,而无需对 PHP 代码进行任何更改,除了在客户端页面中包含 JavaScript。
-
我可以通过对我自己的服务器(当然是CORS)进行一次ajax命中来进一步最小化客户端的代码,这将返回html和脚本等,这些将被注入到html中。因为我不想在它与 wordpress 等集成时做出巨大的改变。这种方法有什么可能的缺点吗?我应该如何实施身份验证?用户将登录到 php 站点,我需要从我的数据库进行身份验证。
-
是的,您可以很容易地在您的服务器上创建一个脚本,将所需的所有内容都拉到客户端页面上。如果脚本从您自己的服务器执行,我认为您甚至不需要担心 CORS,因为它不是跨站点脚本(包含的脚本归您的域所有),我认为这也意味着您可以利用您的身份验证也是如此,您是否会使用包含的脚本通过对您的域的请求对它们进行身份验证?身份验证位有点复杂,我不确定那个。也许其他人可以提供帮助。
标签: php asp.net asp.net-mvc wordpress signalr