Skip to content

Instantly share code, notes, and snippets.

@vishr
Created August 20, 2012 15:23
Show Gist options
  • Save vishr/3405127 to your computer and use it in GitHub Desktop.
Save vishr/3405127 to your computer and use it in GitHub Desktop.

Revisions

  1. Vishal Rana renamed this gist Aug 20, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Vishal Rana created this gist Aug 20, 2012.
    54 changes: 54 additions & 0 deletions client.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <!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>
    41 changes: 41 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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();
    }
    });
    });