From 55d1f2bbea67824754cc7752352434f4cb90a657 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 22 Apr 2015 12:52:40 +0200 Subject: Fixed persistence, removed users persist because wtf --- .gitignore | 2 ++ chatserver.js | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 6477552..57a810c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules/ persist/ +chatserver.out.txt +chatserver.err.txt diff --git a/chatserver.js b/chatserver.js index 69ee637..0d9a30c 100755 --- a/chatserver.js +++ b/chatserver.js @@ -1,6 +1,7 @@ #!/usr/bin/env node var fs=require("fs"), http=require("http"), + util=require("util"), WebSocketServer=require("ws").Server, CircularBuffer=require("circular-buffer"), Persist=require("node-persist"); @@ -18,35 +19,28 @@ function lobby_create(lname){ lobbies.push({id:uniqid(),name:lname,users:[],history:new CircularBuffer(10)}); } -var __do_persist_users_timeout=null; -function do_persist_users(){ - if(__do_persist_users_timeout)return; - __do_persist_users_timeout=setTimeout(function(){ - __do_persist_users_timeout=null; - Persist.setItem("users",users); - },10*1000); -} var __do_persist_lobbies_timeout=null; function do_persist_lobbies(){ if(__do_persist_lobbies_timeout)return; __do_persist_lobbies_timeout=setTimeout(function(){ __do_persist_lobbies_timeout=null; - Persist.setItem("lobbies",lobbies); + Persist.setItem("lobbies",lobbies,function(err,res){ + if(err)console.log("persist lobbies error: "+err); + else console.log("persist lobbies: "+res); + }); },10*1000); } +var users=[],lobbies; Persist.initSync({}); -users=Persist.getItemSync("users"); -if(!users){ - users=[]; -} lobbies=Persist.getItemSync("lobbies"); if(!lobbies){ lobbies=[]; ["lobby_1","lobby_2"].forEach(lobby_create); } else { - lobbies=lobbies.forEach(function(l){ + lobbies.forEach(function(l){ + l.users=[]; l.history=new CircularBuffer(l.history); }); } @@ -93,7 +87,6 @@ new WebSocketServer({port:WSPORT}).on("connection",function(ws){ lobbysend(lobbies[flob(uobj.lob)],"#nick "+uobj.nick+" "+choice); } uobj.nick=choice; - do_persist_users(); do_persist_lobbies(); } else if(msg.match(/^#lobbylist/)){ ws.send("#lobbylist "+lobbies.map(function(lob){return lob.id+":"+lob.name;}).join(" ")); @@ -129,7 +122,6 @@ new WebSocketServer({port:WSPORT}).on("connection",function(ws){ lobbies[lobidx].users.splice(lobfuser(lobbies[lobidx],uobj.id),1); } users.splice(fuser(uobj.id),1); - do_persist_users(); do_persist_lobbies(); }); }); -- cgit v1.2.3-54-g00ecf