繁体   English   中英

IIS 7.5下的SignalR Move Shape演示中未定义客户端属性?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM