2

与其使用 Redis 之类的,甚至 LokiJS(看起来很棒),我可以在我的应用程序运行时在内存中创建一个大型 javascript 集合,然后查询它吗?

4

1 回答 1

0

我有一个使用 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/

这是一款非常受欢迎的游戏。现在,他们为什么会在比赛时将您的位置、分数或姓名存储在数据库中?这将是非常昂贵的。

于 2015-10-30T12:19:44.080 回答