[英]SignalR webapp under IIS7.5 don't works on Firefox access from other webapp(cross-domain)
[英]client property undefined in SignalR Move Shape demo under IIS 7.5?
我在使SignalR演示Move Shape在IIS 7.5上運行時遇到麻煩。 它適用於我在IIS Express和Visual Studio 2013下的開發PC上。
我目前正在將Move.html文件和/ scripts從項目復制到IIS 7.5 PC上的wwwroot目錄。
當我從http:// localhost/Move.html
在IIS PC上加載http:// localhost/Move.html
,在javascript中出現以下錯誤:
未捕獲的TypeError:無法讀取未定義的屬性“ client”
這是我上一行var moveShapeHub = $.connection.moveShapeHub,
的結果var moveShapeHub = $.connection.moveShapeHub,
返回moveShapeHub為未定義。
Move.html:
<!DOCTYPE html>
<html>
<head>
<title>SignalR MoveShape Demo</title>
<style>
#shape {
width: 100px;
height: 100px;
background-color: #FF0000;
}
</style>
</head>
<body>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery-ui-1.10.3.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="/signalr/hubs"></script>
<script>
$(function () {
var moveShapeHub = $.connection.moveShapeHub,
$shape = $("#shape"),
// Send a maximum of 10 messages per second
// (mouse movements trigger a lot of messages)
messageFrequency = 10,
// Determine how often to send messages in
// time to abide by the messageFrequency
updateRate = 1000 / messageFrequency,
shapeModel = {
left: 0,
top: 0
},
moved = false;
moveShapeHub.client.updateShape = function (model) {
shapeModel = model;
// Gradually move the shape towards the new location (interpolate)
// The updateRate is used as the duration because by the time
// we get to the next location we want to be at the "last" location
// We also clear the animation queue so that we start a new
// animation and don't lag behind.
$shape.animate(shapeModel, { duration: updateRate, queue: false });
};
$.connection.hub.start().done(function () {
$shape.draggable({
drag: function () {
shapeModel = $shape.offset();
moved = true;
}
});
// Start the client side server update interval
setInterval(updateServerModel, updateRate);
});
function updateServerModel() {
// Only update server if we have a new movement
if (moved) {
moveShapeHub.server.updateModel(shapeModel);
moved = false;
}
}
});
</script>
<div id="shape" />
</body>
</html>
因此,它能夠找到/ signalr / hubs和其他定義,但無法在IIS 7.5下解析該集線器,但它在IIS express下有效。
我是否缺少IIS 7.5下的某些設置?
在IIS 7.5下需要哪些設置步驟?
可以在IIS 7.5下工作嗎?
這是中心代碼(直接來自演示):
using System;
using System.Threading;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
namespace MoveShapeDemo
{
public class Broadcaster
{
private readonly static Lazy<Broadcaster> _instance =
new Lazy<Broadcaster>(() => new Broadcaster());
// We're going to broadcast to all clients a maximum of 25 times per second
private readonly TimeSpan BroadcastInterval =
TimeSpan.FromMilliseconds(40);
private readonly IHubContext _hubContext;
private Timer _broadcastLoop;
private ShapeModel _model;
private bool _modelUpdated;
public Broadcaster()
{
// Save our hub context so we can easily use it
// to send to its connected clients
_hubContext = GlobalHost.ConnectionManager.GetHubContext<MoveShapeHub>();
_model = new ShapeModel();
_modelUpdated = false;
// Start the broadcast loop
_broadcastLoop = new Timer(
BroadcastShape,
null,
BroadcastInterval,
BroadcastInterval);
}
public void BroadcastShape(object state)
{
// No need to send anything if our model hasn't changed
if (_modelUpdated)
{
// This is how we can access the Clients property
// in a static hub method or outside of the hub entirely
_hubContext.Clients.AllExcept(_model.LastUpdatedBy).updateShape(_model);
_modelUpdated = false;
}
}
public void UpdateShape(ShapeModel clientModel)
{
_model = clientModel;
_modelUpdated = true;
}
public static Broadcaster Instance
{
get
{
return _instance.Value;
}
}
}
public class MoveShapeHub : Hub
{
// Is set via the constructor on each creation
private Broadcaster _broadcaster;
public MoveShapeHub()
: this(Broadcaster.Instance)
{
}
public MoveShapeHub(Broadcaster broadcaster)
{
_broadcaster = broadcaster;
}
public void UpdateModel(ShapeModel clientModel)
{
clientModel.LastUpdatedBy = Context.ConnectionId;
// Update the shape model within our broadcaster
_broadcaster.UpdateShape(clientModel);
}
}
public class ShapeModel
{
// We declare Left and Top as lowercase with
// JsonProperty to sync the client and server models
[JsonProperty("left")]
public double Left { get; set; }
[JsonProperty("top")]
public double Top { get; set; }
// We don't want the client to get the "LastUpdatedBy" property
[JsonIgnore]
public string LastUpdatedBy { get; set; }
}
}
這是Startup.cs:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(MoveShapeDemo.Startup))]
namespace MoveShapeDemo
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
啟動網站后,您可以檢查是否為/ Signalr / hubs加載了中心代理。 只需單擊您的URL http:// localhost / SignalR / hubs。您應該看到SignalR生成的代理(除非您已在Global.asax中將其關閉,但它看起來並不像它,因為您可以在IISExpress中對其進行擊中。
如果您可以啟動集線器代理,則只需在HTML中更改以下內容即可:
<script src="/signalr/hubs"></script>
to
<script src="http:/localhost/signalr/hubs"></script>
您可以在此處了解有關SignalR的更多信息-關於signalR的所有內容的真正好資源。
您說在Visual Studio和IIS中運行時表示Move.html可以正常工作。 作為Move.html的一部分,Visual Studio解決方案或項目中還有什么? 您的集線器有C#代碼,您已經部署了嗎? / signalr / hubs的使用表明Move.html和集線器是同一項目的一部分,但是在原始查詢中,您說只是移動Move.html。 您將必須發布整個項目,然后將發布的版本移至wwwroot文件夾。 有關發布步驟,請點擊此處
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.