aboutsummaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorEv Bogue <ev@evbogue.com>2019-04-03 20:03:30 -0500
committerEv Bogue <ev@evbogue.com>2019-04-03 20:03:30 -0500
commit8856e4b188eadcf286c07e04e7478f2050af7d70 (patch)
tree030fe146f3e1f574500f83ad7db0ad3731a5638c /server.js
initial commit
Diffstat (limited to 'server.js')
-rw-r--r--server.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/server.js b/server.js
new file mode 100644
index 0000000..d3b8f04
--- /dev/null
+++ b/server.js
@@ -0,0 +1,58 @@
+// static server (8089)
+
+var http = require('http')
+var serve = require('ecstatic')
+
+http.createServer(
+ serve({ root: __dirname})
+).listen(8089)
+
+// websocket server (8080)
+
+
+var WebSocket = require('ws')
+var fs = require('fs')
+
+var wss = new WebSocket.Server({ port: 8080 })
+
+wss.on('connection', function (ws) {
+ ws.on('message', function (message) {
+ var receivedLog = JSON.parse(message)
+ if (receivedLog.publicKey) {
+ var publicKey = receivedLog.publicKey
+
+ var clientLog = receivedLog.log
+ // check to see if log is on server
+ if (fs.existsSync(__dirname + '/bogs/' + publicKey)) {
+ var serverLog = JSON.parse(fs.readFileSync(__dirname + '/bogs/' + receivedLog.publicKey))
+ // if the server log has more entries than the log in the client, send the server log to the client
+ if (serverLog.length > clientLog.length) {
+ sendingLog = {
+ publicKey: publicKey,
+ log: serverLog
+ }
+ ws.send(JSON.stringify(sendingLog))
+ console.log('SENT LOG TO CLIENT')
+ }
+ // if server log has less entries than the log sent by the client, write it to the server
+ if (serverLog.length < clientLog.length) {
+ fs.writeFile(__dirname + '/bogs/' + publicKey, JSON.stringify(clientLog), function (err) {
+ if (err) throw err
+ console.log('SAVED LOG TO SERVER')
+ })
+ }
+ // if logs are identical, do nothing
+ if (serverLog.length == clientLog.length) {
+ console.log('LOGS ARE THE SAME')
+ }
+ // if log doesn't already exist, write it
+ } else {
+ fs.writeFile(__dirname + '/bogs/' + publicKey, JSON.stringify(clientLog), function (err) {
+ if (err) throw err
+ console.log('SAVED LOG TO SERVER')
+ })
+ }
+ }
+ })
+})
+