me@helium 8 лет назад
Родитель
Сommit
d5cdfc6204
1 измененных файлов с 41 добавлено и 16 удалено
  1. 41 16
      chatServer/server.js

+ 41 - 16
chatServer/server.js

@@ -1,10 +1,20 @@
-//to chat: http://codepen.io/anon/pen/RoaJZG?editors=0010
-//http://js.do/code/flexichat
+//to chat: http://js.do/code/flexichat
 http = require('http');
 var history = [];
 var messageId = 0;
+
+var RPCFuncs = {
+    getMessages: function(data){
+        var messageId = +data.messageId;
+        return history.slice(messageId);
+    },
+    addMessage:  function(data){
+        history.push(data);
+//        return history.length;
+    }
+};
+
 server = http.createServer(function(req, res){
-    //console.dir(req);
     if (req.method == "POST"){
         var body = '';
         req.on('data', function (data) {
@@ -12,23 +22,38 @@ server = http.createServer(function(req, res){
         });
         req.on('end', function () {
                 console.log("Body: " + body);
-                message = JSON.parse(body);
+                data = JSON.parse(body);
                 var timestamp =(new Date()).getTime(); 
-                if (!("messageId" in message) || (Object.keys(message).length > 1)){
-                    message.timestamp  = timestamp;
-                    history[messageId] = message;
-                    messageId++;
-                }
-                if ("messageId" in message){
-                    messages = history.slice(+message.messageId);
-//                    console.log("slice: " + messages.length)
-                    res.end(JSON.stringify({timestamp: timestamp, messageId: messageId, messages: messages}))
-                    console.log({timestamp: timestamp, messageId: messageId, messages: messages})
+                if ("func" in data){
+                    if (data.func in RPCFuncs){
+                        var func = data.func;
+                        delete data.func;
+                        data.timestamp = timestamp;
+                        var result = {data: RPCFuncs[func](data), nextMessageId: history.length};
+                        res.end(JSON.stringify(result));
+                    }
+                    else {
+                        console.log("no func in functions array" + body);
+                    }
                 }
                 else {
-                    res.end(JSON.stringify({timestamp: timestamp, messageId: messageId}))
-                    console.log({timestamp: timestamp, messageId: messageId})
+                    console.log("no func key in data:" + body);
                 }
+
+                //if (!("messageId" in message) || (Object.keys(message).length > 1)){
+                    //message.timestamp  = timestamp;
+                    //history[messageId] = message;
+                    //messageId++;
+                //}
+                //if ("messageId" in message){
+                    //messages = history.slice(+message.messageId);
+                    //res.end(JSON.stringify({timestamp: timestamp, messageId: messageId, messages: messages}))
+                    //console.log({timestamp: timestamp, messageId: messageId, messages: messages})
+                //}
+                //else {
+                    //res.end(JSON.stringify({timestamp: timestamp, messageId: messageId}))
+                    //console.log({timestamp: timestamp, messageId: messageId})
+                //}
         });
 
         res.setHeader('Access-Control-Allow-Origin', '*');