php – 如何将包含signalR的asp.net mvc页面与其他平台集成
问题不是
this的重复,虽然提到的问题有一些非常有趣的答案,并帮助我更好地理解我的问题,但它不能满足我的需要.让我解释一下我的情景.
我有一个asp.net mvc网站,通过signalR& amp;具有通知功能和实时数据更新. SQL依赖.使用Identity 2.0进行用户身份验证. 现在我想实现以下目标. >简单部分:在其他网站中显示页面内容(仪表板),无论其开发语言如何.它可以注入现有页面或其他(其他网站)所需的位置. 在这种情况下,最佳的行动方案是什么? 更新 我有一个运行在localhost:54603的asp.net mvc网站,以下是家庭控制器的动作 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> 该页面按预期正常运行,并在控制台中记录连接ID. 现在我已经创建了另一个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抛出错误.以下是控制台消息.
我做错了什么导致了这个错误? 解决方法
很难准确确定你需要做什么,因为这个问题相当广泛,但我希望这些要点有点帮助(ed):
SignalR cross-domain在客户端使用JavaScript,在服务器端使用CORS,因此您可以在这方面使用.您可以轻松创建一个可以连接到SignalR通道的JavaScript文件,而无需对PHP代码进行任何更改,只需在客户端页面中包含JavaScript即可. 您可以非常轻松地在服务器上创建一个脚本,客户端可以在其页面上引用该脚本,将所需的所有内容提取到客户端页面上.因为脚本是从您自己的服务器执行的,所以我认为您不必担心CORS,因为它不是真正的“跨站点脚本”,因为执行请求的代码将由您的服务器/域“拥有”. 至于身份验证,如果只有你的脚本需要访问身份验证,我认为这意味着你也可以使用你的域的身份验证(cookie等),但我必须检查那个.最终,您是否使用包含脚本的域名HTTPs请求对它们进行身份验证? 以下是一些有用的线程,可能有助于这一点: > Cross-Domain Cookies (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |