我想制作一个四人连线游戏,并可以选择对战通过本地主机(在同一WiFi内)托管的连接到我的游戏的另一个人.可以在两台计算机上玩游戏,但不能彼此对抗.
首先,我想通过ajax调用从一个客户端发送到另一个客户端的简单字符串.我的尝试:
$(".column").click(function () {
$.ajax({
url: "GetBoard",
method: "GET",
data: { "currentplayer" : currentPlayer },
success: function (data) {
alert("success");
}
});
)};
我使用的控制器方法:
[HttpPost]
public ActionResult GetBoard(string currentPlayer)
{
return View(currentPlayer);
}
我不太了解如何使用ajax发送数据或如何在控制器中接收和使用它.
解决方法:
为了使应用程序可以通过wifi网络使用,您可以执行以下步骤.
第1步
Publish您在本地主机上的应用程序,以便如果其他用户在同一网络上,则他们可以使用侦听端口浏览本地主机.
示例:本地主机:8733
第2步
您可以使用SignalR实时广播游戏中的更改,以便同一本地主机上的其他用户可以看到更改.
例
如果要广播特定ID的移动,则可以像这样创建中心类
public class GameHub : Hub
{
public void Send(string move, int Id)
{
// Call the addNewMessagetoPage method to update clients.
Clients.All.addNewMessagetoPage(move,Id);
}
}
HTML
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
//this will write to page
chat.client.addNewMessagetoPage = function (move, Id) {
console.log("New move "+move);
console.log("New Id "+Id);
};
// Start the connection.
$.connection.hub.start().done(function () {
//this will broadcast move to all pages
$('#broadcast').click(function () {
chat.server.send($('#currentmove').val(), $('#currentId').val());
});
});
});
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。