【发布时间】:2016-05-31 14:05:58
【问题描述】:
我正在尝试将所有用户同时重定向到游戏页面,但我没有成功。我怎样才能同步它们?
我尝试使用 JAVASCRIPT 中的 eventSource,但仍然无法将它们全部放入游戏中。一次只重定向一个。是否有任何功能、框架或其他东西可用于同时重定向所有玩家?
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += " <?php startGame(); ? >";
}
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
【问题讨论】:
-
php 在服务器上运行。假设这个
<script>标记来自服务器上的 .php 页面,那么 startgame() 调用将长期消失并且永远不会到达客户端。只有该函数所做的任何输出才会显示出来。 -
您必须为消息设置一个事件处理程序。 For example:
source.addEventListener('message', function(e) { console.log(e.data); }, false); -
你可能要考虑研究 ajax 请求,让 ajax 请求轻松进行的事情之一是 jquery jQuery.Ajax
-
您需要将
ajax或websockets与PHP 一起使用,因为PHP 是一种server-side编程语言。 -
您可以使用Ratchet 找到您要查找的内容。 PHP 中的 websockets 库。
标签: javascript php html multiplayer