如何更新模型的参数并立即查看?

时间:2019-03-24 21:57:06

标签: javascript c# asp.net-mvc model-view-controller

我正在使用Visual Studio,并使用MVC方法创建了一个Web应用程序。

在我称为“游戏”的视图中,我用Javascript创建了一个画布,其中包含一个正方形元素。我可以将其移动到其他位置。 我想知道它是每时每刻的位置(就像查看鼠标的坐标一样),但是在另一个称为“详细信息”的视图上。

是否有比将每次移动的位置发布到方形模型更好的方法,然后在“详细信息”视图中设置一个间隔函数,使每10毫秒获取一次?

这有点用,但是它非常慢,并且不能真正代表我的正方形元素的实时细节

1 个答案:

答案 0 :(得分:0)

您可以使用ASP.NET SignalR实时更新形状坐标,而不会通过POST和GET请求向服务器发送垃圾邮件。这是使用SignalR移动形状的不错的sample

namespace MoveShapeDemo
{
    public class MoveShapeHub : Hub
    {
        public void UpdateModel(ShapeModel clientModel)
        {
            clientModel.LastUpdatedBy = Context.ConnectionId;
            // Update the shape model within our broadcaster
            Clients.AllExcept(clientModel.LastUpdatedBy).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; }
    }
}

在客户端:

<!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.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.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;
            $shape.css({ left: model.left, top: model.top });
        };
        $.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>