f4c16ff6c86519a0566697135c2e1255a6a1d16a
[platform/framework/web/wrtjs.git] / d2d_app / service / relay-server.js
1 "use strict";
2 var WebSocketServer = require('ws').Server;
3 const serviceWsClientIp = "1";
4 const TO_ALL = 100;
5 let wsClients = [];
6 let serviceWs = [];
7
8 var RelayServer = function(httpserver, options) {
9     var wsServer = new WebSocketServer({ server : httpserver });
10
11     wsServer.on('connection', function(ws, req) {
12         // In case of local client, remoteAddress will be ::1
13         // In case of remote client, remoteAddress will be ::ffff:ipaddress
14         // e.g.) ::ffff:192.168.0.21
15         const rawIp = req.socket.remoteAddress;
16         const ip = rawIp.slice(rawIp.lastIndexOf(":") + 1);
17         const pkgId = req.url.slice(1); // get the substring after '/' from req.url
18
19         if (serviceWs[pkgId] === undefined) {
20             serviceWs[pkgId] = null;
21         }
22         if (wsClients[pkgId] === undefined) {
23             wsClients[pkgId] = [];
24         }
25         if (ip === serviceWsClientIp || ip === '127.0.0.1') {
26             console.log("connected from local");
27             serviceWs[pkgId] = ws;
28             if (!wsClients[pkgId].length)
29                 console.log("connected : no client-clients");
30             ws.on('message', function(msg) {
31                 console.log("msg[" + msg + "]");
32                 const res_msg = 'Success to send : ' + msg;
33
34                 let myPkgId = null;
35                 for (let key in serviceWs) {
36                     if (serviceWs[key] === ws) {
37                         myPkgId = key;
38                         break;
39                     }
40                 }
41
42                 let id = JSON.parse(msg).id;
43                 if (id == -1) {
44                     // Do what need to handle in relay server only
45                     return;
46                 }
47                 if (!wsClients[myPkgId].length)
48                     return;
49                 if (id == TO_ALL) {
50                     for (let client of wsClients[myPkgId])
51                         client.send(msg);
52                 } else {
53                     if (id < wsClients[myPkgId].length)
54                         wsClients[myPkgId][id].send(msg);
55                 }
56             });
57             ws.on('close', function(msg) {
58                 console.log("close server-client");
59             });
60         } else {
61             console.log("connected from", ip);
62             if (wsClients[pkgId].indexOf(ws) == -1) {
63                 wsClients[pkgId].push(ws);
64                 let index = wsClients[pkgId].length - 1;
65                 let res = JSON.stringify({type: "id", data: index, id: -1});
66                 ws.send(res);
67                 if (serviceWs[pkgId] === null)
68                     console.log("connected : no server-client")
69                 else {
70                     serviceWs[pkgId].send(JSON.stringify({type: "new_client", data: index, id: -1}));
71                 }
72             }
73             ws.on('message', function(msg) {
74                 console.log("msg[" + msg + "]");
75                 const res_msg = 'Success to send : ' + msg;
76
77                 let myPkgId = null;
78                 for (let key in wsClients) {
79                     if (wsClients[key].indexOf(ws) != -1) {
80                         myPkgId = key;
81                         break;
82                     }
83                 }
84
85                 if (serviceWs[myPkgId])
86                     serviceWs[myPkgId].send(msg);
87             });
88             ws.on('close', function(msg) {
89                 console.log("close client-clients");
90                 let myPkgId = null;
91                 for (let key in wsClients) {
92                     if (wsClients[key].indexOf(ws) != -1) {
93                         myPkgId = key;
94                         break;
95                     }
96                 }
97
98                 let index = wsClients[myPkgId].indexOf(ws);
99                 wsClients[myPkgId].splice(index, 1);
100                 serviceWs[myPkgId].send(JSON.stringify({type: "client_disconnect", data: {totalcount: wsClients[myPkgId].length, id: index}, id: -1}));
101             });
102         }
103     });
104 };
105
106 module.exports = RelayServer;