Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
Tomás Senart committed Dec 4, 2010
1 parent d8b3932 commit d806ef7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
20 changes: 7 additions & 13 deletions bitbasketd.js
@@ -1,7 +1,7 @@
var sys = require('sys'),
path = require('path'),
express = require('express'),
ws = require('websocket-server'),
ws = require('socket.io'),
uuid = require('uuid'),
redis = require('redis'),
_ = require('underscore')._,
Expand All @@ -16,6 +16,7 @@ var app = express.createServer();
app.configure(function() {
app.use(express.staticProvider(__dirname + '/public'));
});
app.listen(PORT);

// app.get('/u/*/*/*', function(req, res) {
// var requester = req.params.pop();
Expand All @@ -25,17 +26,15 @@ app.configure(function() {
// console.log('Served ' + filename);
// });

var server = ws.createServer({
server: app
});
var server = ws.listen(app);

server.on('connection', function(client) {
var id = uuid.generate();
CLIENTS[id] = client;
client.send(JSON.stringify({
uid: id
}));
console.log('Got new client from "' + client._req.connection.remoteAddress + '" called "' + id + '"');
console.log('Got new client called "' + id + '"');
DB.keys('clients:*:files', function(err, data) {
if (err || !data) return;
_(data.toString('utf8').split(',')).forEach(function(key) {
Expand All @@ -58,7 +57,7 @@ server.on('connection', function(client) {
});
});

client.on('message', function(data) {
client.on('message', function(data) {
var msg = JSON.parse(data);
msg.length = _(msg).keys().length;

Expand Down Expand Up @@ -109,7 +108,7 @@ server.on('connection', function(client) {
});

// BEGIN CLIENT DISCONNECT
client.on('close', function() {
client.on('disconnect', function() {
console.log('Client ' + id + ' disconnected. Deleting his files and broadcasting.');
delete CLIENTS[id];
DB.del('clients:' + id + ':files')
Expand All @@ -120,9 +119,4 @@ server.on('connection', function(client) {
});
// END CLIENT DISCONNECT

});

server.on('shutdown', function(err) {
DB.flushdb();
});
server.listen(PORT);
});
1 change: 1 addition & 0 deletions public/index.html
Expand Up @@ -7,6 +7,7 @@
<link href="css/bitbasket.css" rel="stylesheet" type="text/css">
<script src="js/underscore.js" type="text/javascript"></script>
<script src="js/raphael-min.js" type="text/javascript"></script>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
</head>
<body>
<script src="js/bitbasket.js" type="text/javascript"></script>
Expand Down
11 changes: 6 additions & 5 deletions public/js/bitbasket.js
@@ -1,4 +1,5 @@
var socket = new WebSocket('ws://' + document.location.host);
var socket = new io.Socket();
socket.connect();
var canvas = Raphael(0, 0, window.innerWidth, window.innerHeight);
var step = 15;
var offset = [0, 0];
Expand Down Expand Up @@ -133,8 +134,8 @@ document.ondrop = function(e) {
};

// NETWORKING
socket.onmessage = function(data) {
var msg = JSON.parse(data.data);
socket.on('message', function(data) {
var msg = JSON.parse(data);
msg.length = _(msg).keys().length;
if(msg.length == 1 && msg.uid) {
uid = msg.uid;
Expand Down Expand Up @@ -175,7 +176,7 @@ socket.onmessage = function(data) {
console.log('Sent file "' + bit.file.name + '" to ' + msg.requester)
}

if(msg.length == 2 && !!msg.file.data && msg.sender) {
if(msg.length == 2 && msg.file && !!msg.file.data && msg.sender) {
console.log('Receiving ' + msg.file.name + ' from ' + msg.sender);
if(false /* For now I can't use the File:Writer API*/) {
window.requestFileSystem(PERSISTENT, msg.file.size, function(fs){
Expand Down Expand Up @@ -217,4 +218,4 @@ socket.onmessage = function(data) {
bits = _(bits).filter(function(b){return b.uid != msg.uid});
console.log('Client "' + msg.uid + '" disconnected from server. Removing his bits...');
}
};
});

0 comments on commit d806ef7

Please sign in to comment.