From af91d6fb3c416e806113c00e0aa4e2c48193a3c0 Mon Sep 17 00:00:00 2001 From: edoardoo Date: Fri, 7 Aug 2015 22:42:56 +0200 Subject: [PATCH] working live update through /show POST --- package.json | 3 ++- public/index.html | 3 +++ public/scripts/functions.js | 12 +++++++++++ server.js | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 server.js diff --git a/package.json b/package.json index 038398b..67b7ba4 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,9 @@ "name": "icfp2015", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "server.js", "dependencies": { + "body-parser": "^1.13.3", "express": "^4.13.3", "socket.io": "^1.3.6" }, diff --git a/public/index.html b/public/index.html index 2b01423..65a34da 100644 --- a/public/index.html +++ b/public/index.html @@ -5,7 +5,10 @@ + + +
diff --git a/public/scripts/functions.js b/public/scripts/functions.js index 9eba67e..23f2461 100644 --- a/public/scripts/functions.js +++ b/public/scripts/functions.js @@ -56,3 +56,15 @@ function setFilled(){ } jsonExample = {"height":15,"width":15,"sourceSeeds":[0],"units":[{"members":[{"x":0,"y":0}],"pivot":{"x":0,"y":0}}],"id":1,"filled":[{"x":2,"y":4},{"x":3,"y":4},{"x":4,"y":4},{"x":5,"y":4},{"x":6,"y":4},{"x":11,"y":4},{"x":2,"y":5},{"x":8,"y":5},{"x":11,"y":5},{"x":2,"y":6},{"x":11,"y":6},{"x":2,"y":7},{"x":3,"y":7},{"x":4,"y":7},{"x":8,"y":7},{"x":11,"y":7},{"x":2,"y":8},{"x":9,"y":8},{"x":11,"y":8},{"x":2,"y":9},{"x":8,"y":9},{"x":2,"y":10},{"x":3,"y":10},{"x":4,"y":10},{"x":5,"y":10},{"x":6,"y":10},{"x":9,"y":10},{"x":11,"y":10}],"sourceLength":100}; + +var socket = io.connect("/"); + /*Initializing the connection with the server via websockets */ +socket.on("message",function(message){ + /* + When server sends data to the client it will trigger "message" event on the client side , by + using socket.on("message") , one cna listen for the ,message event and associate a callback to + be executed . The Callback function gets the dat sent from the server + */ + console.log("Message from the server arrived") + console.log(message); /*converting the data into JS object */ +}); diff --git a/server.js b/server.js new file mode 100644 index 0000000..cdbaf12 --- /dev/null +++ b/server.js @@ -0,0 +1,43 @@ +var express = require('express'); +var http = require('http'); +var io = require('socket.io'); +var bodyParser = require('body-parser'); + + +var app = express(); + +app.use(express.static('./public')); +//Specifying the public folder of the server to make the html accesible using the static middleware + + +var jsonParser = bodyParser.json() + +// create application/x-www-form-urlencoded parser +var urlencodedParser = bodyParser.urlencoded({ extended: true }) + +// POST /login gets urlencoded bodies + + + + +var server = http.createServer(app).listen(8080); +console.log("Listening on port 8080"); +console.log("Send jsons to /show please.") +//Server listens on the port 8124 +io = io.listen(server); +/*initializing the websockets communication , server instance has to be sent as the argument */ + +io.sockets.on("connection",function(socket){ + /*Associating the callback function to be executed when client visits the page and + websocket connection is made */ + console.log("New Connection"); + + + app.post('/show', urlencodedParser, function (req, res) { + + res.send('200'); + console.log("sending porcoddio"); + socket.emit("message", "porcoddio"); + }); + +}); \ No newline at end of file