Browse Source

working live update through /show POST

visualizer
edoardoo 9 years ago
committed by Slash
parent
commit
af91d6fb3c
4 changed files with 60 additions and 1 deletions
  1. +2
    -1
      package.json
  2. +3
    -0
      public/index.html
  3. +12
    -0
      public/scripts/functions.js
  4. +43
    -0
      server.js

+ 2
- 1
package.json View File

@ -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"
},


+ 3
- 0
public/index.html View File

@ -5,7 +5,10 @@
<!-- <link rel="stylesheet" type="text/css" href="style/style.css"> -->
<link rel="stylesheet/less" type="text/css" href="style/style.less" />
<script type="text/javascript" src="scripts/less.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script type="text/javascript" src="scripts/functions.js"></script>
</head>
<body>
<div id="control"><button onclick="updateDimensions()">R</button></div>


+ 12
- 0
public/scripts/functions.js View File

@ -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 */
});

+ 43
- 0
server.js View File

@ -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");
});
});

Loading…
Cancel
Save