该让数据流走哪条路?

我要开始一个HTML5游戏,该游戏具有Java脚本中的大量逻辑,我想在服务器端保留一些逻辑,以便保证游戏只能在我的服务器上玩。

我决定选择node.js,因为它非常快,所以我考虑了两种方法:

1、使用AJAX,客户端将调用服务器端方法,该方法将返回计算出的数字以刷新游戏场景,此调用将每2秒调用一次。

2、使用node.js打开套接字,以便客户端不必每次都调用服务器,而是继续监听从打开的套接字流式传输的数据,该套接字每隔x秒刷新一次数据。 计算出的数据并不大,大约每秒0.5 kb,客户端也需要告诉服务器状态如何,因此从客户端发送的数据大约为0.1 kb / x秒,这取决于游戏的玩法。

第二种方法似乎更好,但是,我将需要数百个端口来处理并发播放器......

那么,在性能和最小化使用带宽方面,该选择哪种方法? 还是有更好的方法吗? 谁能帮忙我?

As you mentioned you are creating a web-based JavaScript application that regularly sends information to, or retrieves updates from, a server then in my opinion you should use WebSocket(especially you are developing in HTML5), which reduce the amount of bandwidth your application uses.

In term of performance, I would chose WebSocket aswell, by doing some measurement experiments e.g averaging a round trip time for 100 requests at a time, WebSocket has a lower round trip time. Here is a link of a performance test might tell the result: http://www.peterbe.com/plog/are-websockets-faster-than-ajax