websocket test init

This commit is contained in:
Luigi Maselli
2013-06-30 23:59:06 +02:00
parent de906a22aa
commit 4d77b668b4
8 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1 @@
web: node app.js

View File

@@ -0,0 +1 @@
node websocket demo

View File

@@ -0,0 +1,75 @@
var express = require('express')
, exphbs = require('express3-handlebars')
, http = require('http')
, connect = require('connect')
, io = require('socket.io');
var app = express();
app.engine('handlebars',exphbs({extname: ".html", layoutsDir:"views/", defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');
/* NOTE: We'll need to refer to the sessionStore container later. To
* accomplish this, we'll create our own and pass it to Express
* rather than letting it create its own. */
var sessionStore = new connect.session.MemoryStore();
/* NOTE: We'll need the site secret later too, so let's factor it out.
* The security implications of this are left to the reader. */
var SITE_SECRET = 'I am not wearing any pants';
// app.configure(function(){
// app.set('view engine', 'handlebars');
// // app.set("view options", { layout: false })
// });
app.set('views', __dirname + '/views');
// app.register('.html', require('handlebars'));
/*
* ... skipping some of your app settings ...
*/
app.use(express.bodyParser());
/* NOTE: Pass the cookieParser the site secret. It used to be that
* express.session() got the secret, but in Express 3 that's
* no longer the case. */
app.use(express.cookieParser(SITE_SECRET));
/* NOTE: We'll need to know the key used to store the session, so
* we explicitly define what it should be. Also, we pass in
* our sessionStore container here. */
app.use(express.session({
key: 'express.sid'
, store: sessionStore
}));
app.use(express.static('public'));
/*
* ... skipping the rest of your app settings ...
*/
app.get('/', function(req, res){
res.render('index', {socket_url:"ciaourl"});
});
var server = http.createServer(app);
var app_port = process.env.PORT || 3000;
server.listen(app_port, function(){
console.log("Express server listening on port "+ app_port);
});
/**
* Socket.io
*/
var sio = io.listen(server);
sio.sockets.on('connection', function (socket) {
// temperature
setInterval(function () {
var val = (new Date).getTime();
socket.emit('new value', { val: "I'm a websocket message: "+val });
}, 2000);
});

View File

@@ -0,0 +1,2 @@
#!/bin/bash
set -e; output="$(curl -s -i -N -H \"Connection: Upgrade\" -H \"Upgrade: websocket\" -H \"Host: $1\" -H \"Origin: $1\" $1 -m 2)"; echo $output; test "$output" == "bazingaa"

View File

@@ -0,0 +1,16 @@
{
"name": "node-websocket-app",
"version": "0.0.0",
"description": "a basic webscket demo",
"author": "Luigi Maselli",
"dependencies": {
"express": "3.1.x",
"connect": "*",
"handlebars": "~1.0.9",
"socket.io": "~0.9.13",
"express3-handlebars": "~0.2.3"
},
"engines": {
"node": "0.10.x"
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,5 @@
<h2>I'm a static HTTP message</h2>
<h1 id="display">
No messages
</h1>
<p>If you get many GET messages in your console it isn't websocket but socket.io fallback mode</p>

View File

@@ -0,0 +1,25 @@
<html>
<head>
<title>Websocket demo Socket.io</title>
<script src="./socket.io/socket.io.js"></script>
<script src="./jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function () {
var socket = io.connect( window.location.origin ); // transparent HTTP / Websocket upgrade
socket.on('new value', function (data) {
console.log("RECV:"+ JSON.stringify(data));
$('#display').html(data.val);
});
});
</script>
{{{body}}}
</body>
</html>