Skip to content

Instantly share code, notes, and snippets.

@tommedema
Created April 13, 2011 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommedema/918288 to your computer and use it in GitHub Desktop.
Save tommedema/918288 to your computer and use it in GitHub Desktop.
Run this: sudo node uploadNowTest.js -> go to http://localhost:3000 in the browser. Select a semi-large file (eg. 3MB) --> upload. When removing the now.js broadcast, this is instant (as it should be, because this is localhost with 0 latency). When broadc
//dependencies:
//node-formidable
//now.js
var formidable = require('formidable'),
http = require('http'),
sys = require('sys'),
util = require('util'),
nowjs = require('now'),
uploadCounter = 0,
everyone = null; //will be set right after server creation
var server = http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
//parse a file upload
var form = new formidable.IncomingForm();
//set properties
form.encoding = 'utf-8';
form.keepExtensions = true; //keep extensions
form.maxFieldsSize = 0.5 * 1024 * 1024; //max memory allocated of a field in bytes
//set event listeners
form
.on('field', function(name, value) {
util.debug('received field: ' + name + ' = ' + value);
})
.on('progress', function(bytesReceived, bytesExpected) {
//form progress update
})
.on('error', function(err) {
util.debug('error: ' + err);
})
.on('end', function() {
util.debug('form stream end');
});
//handle every part
form.onPart = function(part) {
util.debug('getting new part...');
//let formidable handle all non-file parts (fields)
if (!part.filename) {
util.debug('letting formidable handle this non-file part');
form.handlePart(part);
return;
}
//increment counter
uploadCounter++;
//start file receival to clients
everyone.now.startFileReceival('id#' + uploadCounter, part.filename, form.bytesExpected, function(success) { //TODO: part.bytesExpected
util.debug('client accepted file receival: ' + success);
});
//handle chunks of files
part
.on('data', function(chunk) {
//send to every now.js client
form.pause();
everyone.now.receiveFileChunk('id#' + uploadCounter, chunk, function(success) {
form.resume();
util.debug('client accepted chunk: ' + success);
});
})
.on('end', function() {
util.debug('part end');
});
};
//start parsing this stream
form.parse(req);
return;
}
//else: show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<html>'+
'<head>'+
'<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>'+
'<script type="text/javascript" src="/nowjs/now.js"></script>'+
'<script type="text/javascript">'+
'$(document).ready(function() {'+
' alert("Hi, this is a now.js test.");'+
' var files = {};'+
' now.startFileReceival = function(id, fileName, bytesExpected, cb) {'+
//' //this file id must be unique'+
' if (typeof(files[id]) !== "undefined") {'+
' cb(false);'+
' return;'+
' }'+
//' //add this file'+
' files[id] = {fileName: fileName, bytesExpected: bytesExpected, data: ""};'+
//' //success'+
' cb(true);'+
' };'+
' now.receiveFileChunk = function(id, chunk, cb) {'+
//' //this id must exist'+
' if (typeof(files[id]) === "undefined") {'+
' cb(false);'+
' return;'+
' }'+
//' //add this chunk'+
' files[id].data += chunk;'+
//' //success'+
' cb(true);'+
' };'+
'});'+
'</script>'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'+
'</body>'+
'</html>'
);
});
server.listen(3000);
everyone = nowjs.initialize(server);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment