简体   繁体   English

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

[英]client property undefined in SignalR Move Shape demo under IIS 7.5?

I'm having trouble getting the SignalR demo Move Shape working on IIS 7.5. 我在使SignalR演示Move Shape在IIS 7.5上运行时遇到麻烦。 It works for me on my development PC under IIS express and Visual Studio 2013. 它适用于我在IIS Express和Visual Studio 2013下的开发PC上。

I'm currently copying the Move.html file and /scripts from the project to the wwwroot directory on the IIS 7.5 PC. 我目前正在将Move.html文件和/ scripts从项目复制到IIS 7.5 PC上的wwwroot目录。

When I load the Move.html on the IIS PC from http:// localhost/Move.html , I get the following error in the javascript: 当我从http:// localhost/Move.html在IIS PC上加载http:// localhost/Move.html ,在javascript中出现以下错误:

Uncaught TypeError: Cannot read property 'client' of undefined 未捕获的TypeError:无法读取未定义的属性“ client”

which is the result of my previous line var moveShapeHub = $.connection.moveShapeHub, returning moveShapeHub as undefined. 这是我上一行var moveShapeHub = $.connection.moveShapeHub,的结果var moveShapeHub = $.connection.moveShapeHub,返回moveShapeHub为未定义。

Move.html: 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>

So it is able to find the /signalr/hubs and the other definitions but can't resolve the hub under IIS 7.5, but it works under IIS express. 因此,它能够找到/ signalr / hubs和其他定义,但无法在IIS 7.5下解析该集线器,但它在IIS express下有效。

Am I missing some setup under IIS 7.5? 我是否缺少IIS 7.5下的某些设置?

What setup steps are needed under IIS 7.5? 在IIS 7.5下需要哪些设置步骤?

Can this work under IIS 7.5? 可以在IIS 7.5下工作吗?

Here is the hub code (straight from the demo): 这是中心代码(直接来自演示):

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; }
    }

}

And here is the Startup.cs: 这是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();
        }
    }
}

Once you launch your site could you check to see if your hub proxy is getting loaded for /Signalr/hubs. 启动网站后,您可以检查是否为/ Signalr / hubs加载了中心代理。 Just hit your URL http:// localhost/SignalR/hubs.You should see the SignalR generated proxy (unless you have turned it off in your Global.asax which it does not look like it because you were able to hit it in IISExpress. 只需单击您的URL http:// localhost / SignalR / hubs。您应该看到SignalR生成的代理(除非您已在Global.asax中将其关闭,但它看起来并不像它,因为您可以在IISExpress中对其进行击中。

If you can bring up the hub proxy up then just change the following in your HTML, from 如果您可以启动集线器代理,则只需在HTML中更改以下内容即可:

    <script src="/signalr/hubs"></script>

                    to

    <script src="http:/localhost/signalr/hubs"></script>

You can learn more about SignalR here - really good resource on everything that is signalR. 您可以在此处了解有关SignalR的更多信息-关于signalR的所有内容的真正好资源。

UPDATE: 更新:

You say that when running in visual studio and IIS expresss your Move.html works just fine. 您说在Visual Studio和IIS中运行时表示Move.html可以正常工作。 What else is in the Visual Studio solution or project as part of Move.html? 作为Move.html的一部分,Visual Studio解决方案或项目中还有什么? You have C# code for your hub, have you deployed that? 您的集线器有C#代码,您已经部署了吗? The use of /signalr/hubs suggests that Move.html and the hub are part of the same project, but in your original query you say that you just moved Move.html. / signalr / hubs的使用表明Move.html和集线器是同一项目的一部分,但是在原始查询中,您说只是移动Move.html。 You would have to publish the entire project and move that published version to the wwwroot folder. 您将必须发布整个项目,然后将发布的版本移至wwwroot文件夹。 For publishing steps look here 有关发布步骤,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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