我有一个使用 socket.io 的具有这种精确模式的应用程序。
下面,我已经翻译了我的 socket.io 代码以使用 HTTP 请求。
在服务器上,你可以这样做:
var players = []; //This will be used to store in-memory players.
他们,
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); //Need this to populate req.body on requests.
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
next(); //Need this to accept incoming requests.
});
app.get('/players', function (req, res) {
res.json(players);
});
app.post('/players', function (req, res) {
//validation
players.push(req.body);
});
[...]
看这个例子:
http://agar.io/
这是一款非常受欢迎的游戏。现在,他们为什么会在比赛时将您的位置、分数或姓名存储在数据库中?这将非常昂贵。