Permalink
Browse files

30 minutes left and still can't get socket.io to work

  • Loading branch information...
1 parent 4128502 commit 1be07344a3cefa1df36fd2dbafe141cfda852f06 Jonas Galvez committed Aug 29, 2010
Showing with 32 additions and 10 deletions.
  1. +11 −5 pagetalk.js
  2. +16 −1 static/js/index.js
  3. +4 −3 static/js/voting-room.js
  4. +1 −1 support.js
View
@@ -37,34 +37,40 @@ http_server.get('/', function(req, res) {
});
http_server.get('/voting-room/:id', function(req, res) {
+ if(!poorsman_mongodb.voting_rooms[req.params.id]) {
+ res.redirect('/');
+ return;
+ }
res.render('voting-room.html', {'locals': {'voting_room_id': req.params.id}});
});
http_server.post('/voting-room', function(req, res) {
+ var voting_room_data = JSON.parse(decodeURIComponent(req.body.voting_room_data));
var voting_room_id = create_random_id(10);
while(poorsman_mongodb.voting_rooms[voting_room_id])
voting_room_id = create_random_id(10);
- var vr = poorsman_mongodb.voting_rooms[voting_room_id] = {};
+ var vr = poorsman_mongodb.voting_rooms[voting_room_id] = voting_room_data;
+ vr.voting_room_id = voting_room_id;
res.redirect('/voting-room/' + voting_room_id);
});
http_server.listen(80);
-socket_server = SocketIO.listen(http_server);
+socket_server = io.listen(http_server);
socket_server.on('connection', function(client) {
poorsman_mongodb.connected_clients[client.sessionId] = {};
client.on('message', function(message) {
- var message = JSON.parse(message) || {};
var voting_room_id = message.voting_room_id;
poorsman_mongodb.connected_clients[client.sessionId] = message;
if(message.pick) {
if(poorsman_mongodb.voting_rooms[voting_room_id][message.pick] == undefined) {
poorsman_mongodb.voting_rooms[voting_room_id][message.pick] = 0;
}
- poorsman_mongodb.voting_rooms[voting_room_id][message.pick]++;
- client.broadcast(poorsman_mongodb.voting_rooms[voting_room_id]);
+ poorsman_mongodb.voting_rooms[voting_room_id][message.pick]++;
}
+ console.log('poorsman_mongodb.voting_rooms[voting_room_id]: ' + poorsman_mongodb.voting_rooms[voting_room_id]);
+ client.broadcast({'omg': 1});
});
client.on('disconnect', function() {
var message = poorsman_mongodb.connected_clients[client.sessionId];
View
@@ -1,7 +1,22 @@
nodevote = {'ui': {'handlers': {}}};
nodevote.ui.handlers['create'] = {
'click': function() {
- console.log('foo');
+ var options = [];
+ var bars = $('.bar');
+ for(var option, i = 0, len = bars.length; i < len; i++) {
+ option = {};
+ option.title = $(bars[i]).find('input').val();
+ option.colors = {
+ 'back': $(bars[i]).css('background-color'),
+ 'fore': $(bars[i]).find('input').css('color')
+ }
+ options.push(option);
+ }
+ var options_json = encodeURIComponent(JSON.stringify({'options': options}));
+ console.log('options_json: ' + options_json);
+ $('#voting-room-form').append(
+ '<input type="hidden" name="voting_room_data" value="' + options_json + '">'
+ );
$('#voting-room-form').submit();
}
}
@@ -4,15 +4,16 @@ nodevote.ui.handlers = {};
$(document).ready(function() {
io.setPath('/voting-room/' + nodevote.voting_room_id);
- var socket_client = new io.Socket(null, {port: 80});
- socket_client.connect();
+ socket_client = new io.Socket(null, {port: 80});
socket_client.on('message', function(options) {
- console.log(message);
+ console.log(options);
options = JSON.parse(options);
});
nodevote.ui.handlers['freeze'] = {
'click': function() {
}
}
+ socket_client.connect();
+ socket_client.send({'voting_room_id': nodevote.voting_room_id});
$('#freeze').click(nodevote.ui.handlers['freeze'].click);
});
View
@@ -9,4 +9,4 @@ require.paths.unshift(
);
express = require('express');
JSON = require('json');
-SocketIO = require('socket.io');
+io = require('socket.io');

0 comments on commit 1be0734

Please sign in to comment.