简体   繁体   English

HTML5游戏<->服务器

[英]HTML5 game <-> server

I have: 我有:

  1. PHP Socket Server PHP套接字服务器

  2. PHP Pages to handle communication with server & db PHP页面处理与服务器和数据库的通信

  3. Client .php page, which uses javascript/canvas to display & interact with a game. 客户端.php页面,该页面使用javascript / canvas显示和与游戏互动。

What I want is for the user to connect and authenticate using the PHP pages which will initiate a socket connection to the server. 我想要的是用户使用PHP页面进行连接和身份验证,这将启动与服务器的套接字连接。 Based on the information from that socket some chat info and events might be triggered to load data into client php page. 根据来自该套接字的信息,可能会触发一些聊天信息和事件,以将数据加载到客户端php页面中。

What is the right way to handle this. 什么是正确的处理方式。 In all instances that I've ajax in before, the user triggers the event, not the server and javascript waits around for the php page's response. 在我之前使用过ajax的所有实例中,用户都会触发事件,而不是服务器,而javascript等待php页面的响应。

What piece of php code from the php socket lets me send messages to the canvas elements? php套接字中的哪段php代码可以让我将消息发送到canvas元素?

Here's an example as the above is probably not very illuminating: 这是一个示例,因为上面的示例可能不太清楚:

IE: PHP reads off of its socket connection {"name":"msg","from":"user1","time":12345,"msg","hello world"} IE:PHP读取了其套接字连接{"name":"msg","from":"user1","time":12345,"msg","hello world"}

At this point what does PHP do?** So that ... 此时,PHP会做什么?**这样...

The client has a div with "id='Incoming'" 客户端的div带有"id='Incoming'"

I want to launch the following javascript: 我要启动以下javascript:

function handleMessage(msgObj) {
  var cText = document.getElementById("Incoming").innerHTML;
  cText += "(" + msgObj.time + ")" + msgObj.from + " says: " + msgObj.msg +"<br/>";
  document.getElementById("Incoming").innerHTML = cText;
}

I guess I'm trying to figure out how a client's web page is supposed to talk back to the php sockets... 我想我想弄清楚客户端的网页应该如何与php套接字对话...

Is this possible or do I need to make a client in flash, java, etc? 这可能吗,还是我需要在Flash,Java等中建立客户端?

You send stuff from client to server when you've got data to send (eg user submits a button) typically via an event handler. 当您有数据要发送(例如,用户提交按钮)时,通常通过事件处理程序将内容从客户端发送到服务器。 The websocket also fires event when it receives data from the server (socket.onmessage). 当websocket从服务器接收数据时(socket.onmessage),也会触发事件。 Simples. 简单。

ws.onmessage=function (evt) { handleMessage(evt.data); };

(or just change your handleMessage function body to use msgObj.data as the text value) (或者只是将handleMessage函数主体更改为使用msgObj.data作为文本值)

What piece of php code from the php socket lets me send messages to the canvas elements? php套接字中的哪段php代码可以让我将消息发送到canvas元素?

What websocket implementation are you using? 您正在使用哪种websocket实现? They all have different APIs (NB a websocket is not the same thing as a network socket) 它们都有不同的API(注意,websocket与网络套接字不一样)

You should use Comet: 您应该使用Comet:
http://en.wikipedia.org/wiki/Comet_(programming ) http://en.wikipedia.org/wiki/Comet_(编程

AJAX is much better and also asynchronous AJAX更好而且异步

http://www.ape-project.org/ http://www.ape-project.org/
http://www.stream-hub.com/ http://www.stream-hub.com/
... ...

http://cometdaily.com/maturity.html http://cometdaily.com/maturity.html

or use HTML5 WebSockets 或使用HTML5 WebSockets

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

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