Skip to content

Instantly share code, notes, and snippets.

@vishr
Created August 20, 2012 15:23
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 vishr/3405127 to your computer and use it in GitHub Desktop.
Save vishr/3405127 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
var sock = null;
var wsuri = "ws://localhost:3000";
window.onload = function() {
sock = new WebSocket(wsuri);
sock.onopen = function() {
console.log("connected to " + wsuri);
}
sock.onclose = function(e) {
console.log("connection closed (" + e.code + ")");
}
sock.onmessage = function(e) {
console.log("message received: " + e.data);
}
}
$("#files").live("change", function(e) {
$.each(e.target.files, function(i, f) {
var chunks = chunkify(f)
$.each(chunks, function(i, c) {
sock.send(c);
});
});
});
function chunkify(file) {
var step = 32 * 1024,
start = 0,
end = step,
chunks = [];
while (end <= file.size + step) {
chunks.push(file.slice(start, end));
start = end;
end += step;
}
return chunks;
}
</script>
</head>
<body>
<form>
<input id="files" type="file" multiple>
</form>
</body>
</html>
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
WebSocketServer = require("ws").Server;
var app = express();
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/', routes.index);
var server = http.createServer(app).listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});
var wss = new WebSocketServer({
server: server
});
wss.on("connection", function(ws) {
var now = Date.now();
ws.on("message", function(data, flags) {
var channel;
if (flags.binary) {
console.log("took: %d", Date.now() - now);
now = Date.now();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment