websocket服务器,存储数据 [英] websocket server, storing the data
问题描述
可以说我进行了一个Websocket服务器聊天(node.js + socket.io).我将如何存储聊天消息,以便当新"用户加入聊天时,他将看到旧的聊天消息,而不仅仅是看到他在聊天中发送的消息.
Lets say that i make a websocket server chat (node.js + socket.io). How would i store the chat messages, so that when a "new" user joins the chat, he will be seeing old chat messages, and not just the ones which has been sent while hes in the chat.
数据应该存储在服务器的变量中吗?像这样:
Should the data be stored in variables in the server? Something like:
var io = require('socket.io').listen(80);
var saveData = { };
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
对于更复杂的应用程序(例如游戏)来说,这是否可行?
Would it be a viable soultion for a more complex application, like a game for example?
谢谢
推荐答案
如果您确实需要为新用户存储聊天记录,我建议将其存储在RDBMS或NoSQL存储中.如果您选择NoSQL,则其中一个选项是 Apache CouchDB™(使用JSON处理文档,JavaScript处理MapReduce查询,以及API的常规HTTP).这是免费的在线书.
If you really need to store chat history for new user I would suggest storing those in RDBMS or NoSQL storage. If you choose NoSQL one of options is Apache CouchDB™ (database that uses JSON for documents, JavaScript for MapReduce queries, and regular HTTP for an API). Here is free online book about it.
这篇关于websocket服务器,存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!