From 7ee75b9e7f29ff2f3b0889eb164178d5ef80dd3f Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Wed, 16 Mar 2016 14:57:13 +0100 Subject: [PATCH] Messages received by the WebSocket server are now saved in a file --- .gitignore | 1 + NetFluxWebsocketServer.js | 3 ++- storage/LogStore.js | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 storage/LogStore.js diff --git a/.gitignore b/.gitignore index 4bfe66739..e4c4f537c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ customization .*.swp *.db /customize/ +messages.log diff --git a/NetFluxWebsocketServer.js b/NetFluxWebsocketServer.js index a1d73fb12..113e8ded9 100644 --- a/NetFluxWebsocketServer.js +++ b/NetFluxWebsocketServer.js @@ -1,6 +1,7 @@ ;(function () { 'use strict'; let Crypto = require('crypto'); let WebSocket = require('ws'); +let LogStore = require('./storage/LogStore'); let LAG_MAX_BEFORE_DISCONNECT = 30000; let LAG_MAX_BEFORE_PING = 15000; @@ -142,7 +143,7 @@ let run = module.exports.run = function (storage, socketServer) { let ctx = { users: {}, channels: {}, - store: storage + store: LogStore.create('messages.log', storage) }; setInterval(function () { Object.keys(ctx.users).forEach(function (userId) { diff --git a/storage/LogStore.js b/storage/LogStore.js new file mode 100644 index 000000000..4f1b7ad6f --- /dev/null +++ b/storage/LogStore.js @@ -0,0 +1,20 @@ + var Fs = require("fs"); + + var message = function(file, msg) { + file.write(msg+"\n"); + } + + var create = module.exports.create = function(filePath, backingStore) { + + var file = Fs.createWriteStream(filePath, {flags: 'a+'}); + + return { + message: function(channel, msg, callback) { + message(file, msg); + backingStore.message(channel, msg, callback); + }, + getMessages: backingStore.getMessages + } + + } +