Person count, visual fixes.
This commit is contained in:
		
							parent
							
								
									9205eb93b0
								
							
						
					
					
						commit
						05a420d0a7
					
				
					 4 changed files with 64 additions and 14 deletions
				
			
		| 
						 | 
					@ -40,6 +40,7 @@ body, html {
 | 
				
			||||||
#chat li {
 | 
					#chat li {
 | 
				
			||||||
	white-space: pre;
 | 
						white-space: pre;
 | 
				
			||||||
	padding: 2px 15px;
 | 
						padding: 2px 15px;
 | 
				
			||||||
 | 
						color: #343434;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Message types */
 | 
					/* Message types */
 | 
				
			||||||
| 
						 | 
					@ -49,9 +50,10 @@ body, html {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#chat i.motd	{ color: #99FF99; display:inline-block; line-height: 12px !important; }
 | 
					#chat i.motd	{ color: #99FF99; display:inline-block; line-height: 12px !important; }
 | 
				
			||||||
#chat i.info	{ color: #999999; }
 | 
					#chat i.info	{ color: #999999; }
 | 
				
			||||||
#chat i.server	{ color: #999999; }
 | 
					#chat i.server	{ color: #99FFFF; }
 | 
				
			||||||
#chat i.error	{ color: #ff7777; }
 | 
					#chat i.error	{ color: #ff7777; }
 | 
				
			||||||
#chat i.message	{ color: #eeeeee; }
 | 
					#chat i.message	{ color: #eeeeee; }
 | 
				
			||||||
 | 
					#chat i.nick	{ color: #99FF99; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*------------------------------------*\
 | 
					/*------------------------------------*\
 | 
				
			||||||
	INPUT
 | 
						INPUT
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,6 +58,15 @@ define('cryptalk', {
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								count: function () {
 | 
				
			||||||
 | 
									if( room ) {
 | 
				
			||||||
 | 
										socket.emit('room:count');
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										post('error', templates.messages.not_in_room);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			key: function (payload) {
 | 
								key: function (payload) {
 | 
				
			||||||
				// Make sure the key meets the length requirements
 | 
									// Make sure the key meets the length requirements
 | 
				
			||||||
				if (payload.length < 8) {
 | 
									if (payload.length < 8) {
 | 
				
			||||||
| 
						 | 
					@ -177,6 +186,9 @@ define('cryptalk', {
 | 
				
			||||||
		.on('room:joined', function (data) {
 | 
							.on('room:joined', function (data) {
 | 
				
			||||||
			room = data;
 | 
								room = data;
 | 
				
			||||||
			post('info', $.template(templates.messages.joined_room, { roomName: room }));
 | 
								post('info', $.template(templates.messages.joined_room, { roomName: room }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Automatically count persons on join
 | 
				
			||||||
 | 
								socket.emit('room:count');
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		.on('room:left', function () {
 | 
							.on('room:left', function () {
 | 
				
			||||||
| 
						 | 
					@ -198,8 +210,21 @@ define('cryptalk', {
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		.on('message:server', function (data) {
 | 
							.on('message:server', function (data) {
 | 
				
			||||||
			var sanitized = $.escapeHtml(data);
 | 
								if( data.msg ) {
 | 
				
			||||||
			post('server', data);
 | 
									var sanitized = $.escapeHtml(data.msg);
 | 
				
			||||||
 | 
									if( templates.server[sanitized] ) {
 | 
				
			||||||
 | 
										if( data.payload !== undefined ) {
 | 
				
			||||||
 | 
											var sanitized_payload = $.escapeHtml(data.payload);
 | 
				
			||||||
 | 
											post('server', $.template(templates.server[sanitized], { payload: sanitized_payload }));
 | 
				
			||||||
 | 
										} else {
 | 
				
			||||||
 | 
											post('server', templates.server[sanitized]);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										post('error', templates.server.bogus);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									post('error', templates.server.bogus);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Post the help/welcome message
 | 
						// Post the help/welcome message
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,9 +26,10 @@ define({
 | 
				
			||||||
		'Available commands:                                                    \n' +
 | 
							'Available commands:                                                    \n' +
 | 
				
			||||||
		' 	/generate 				Generate random room                        \n' +
 | 
							' 	/generate 				Generate random room                        \n' +
 | 
				
			||||||
		'	/join		RoomId			Join a room	                            \n' +
 | 
							'	/join		RoomId			Join a room	                            \n' +
 | 
				
			||||||
		'	/leave					Leave the room                              \n' +
 | 
							'	/count					Count participants of room                  \n' +
 | 
				
			||||||
		'	/nick		NickName		Sets an optional nick                   \n' +
 | 
							'	/nick		NickName		Sets an optional nick                   \n' +
 | 
				
			||||||
		'	/key		OurStrongPassphrase	Sets encryption key                 \n' +
 | 
							'	/key		OurStrongPassphrase	Sets encryption key                 \n' +
 | 
				
			||||||
 | 
							'	/leave					Leave the room                              \n' +
 | 
				
			||||||
		'	/clear					Clear on-screen buffer                      \n' +
 | 
							'	/clear					Clear on-screen buffer                      \n' +
 | 
				
			||||||
		'	/help					This                                        \n' +
 | 
							'	/help					This                                        \n' +
 | 
				
			||||||
		'                                                                       \n' +
 | 
							'                                                                       \n' +
 | 
				
			||||||
| 
						 | 
					@ -43,10 +44,10 @@ define({
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	post: {
 | 
						post: {
 | 
				
			||||||
		motd: 		'<li><i class="motd">{text}</i></li>',
 | 
							motd: 		'<li><i class="motd">{text}</i></li>',
 | 
				
			||||||
		info: 		'<li>INF> <i class="info">{text}</i></li>',
 | 
							info: 		'<li>INF> <i class="info">{text}</i></li>',
 | 
				
			||||||
		server: 	'<li>SRV> <i class="server">{text}</i></li>',
 | 
							server: 	'<li>SRV> <i class="server">{text}</i></li>',
 | 
				
			||||||
		error: 		'<li>ERR> <i class="error">{text}</i></li>',
 | 
							error: 		'<li>ERR> <i class="error">{text}</i></li>',
 | 
				
			||||||
		message: 	'<li>{nick}> <i class="message">{text}</i></li>'
 | 
							message: 	'<li><i class="nick">{nick}></i> <i class="message">{text}</i></li>'
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	messages: {
 | 
						messages: {
 | 
				
			||||||
| 
						 | 
					@ -54,6 +55,7 @@ define({
 | 
				
			||||||
		key_ok_ready: 			'Key set, you can now start communicating.',
 | 
							key_ok_ready: 			'Key set, you can now start communicating.',
 | 
				
			||||||
		key_ok_but_no_room: 	'Key set, you can now join a room and start communicating.',
 | 
							key_ok_but_no_room: 	'Key set, you can now join a room and start communicating.',
 | 
				
			||||||
		msg_no_room: 			'You have to join a room before sending messages. See /help.',
 | 
							msg_no_room: 			'You have to join a room before sending messages. See /help.',
 | 
				
			||||||
 | 
							not_in_room: 			'You have to be in a room to count participants...',
 | 
				
			||||||
		msg_no_key: 			'You have to set an encryption key before sending a message. See /help.',
 | 
							msg_no_key: 			'You have to set an encryption key before sending a message. See /help.',
 | 
				
			||||||
		nick_short: 			'Nickname is too short, try again.',
 | 
							nick_short: 			'Nickname is too short, try again.',
 | 
				
			||||||
		nick_set: 				'From now on, you\'re referred to as \'{nick}\'.',
 | 
							nick_set: 				'From now on, you\'re referred to as \'{nick}\'.',
 | 
				
			||||||
| 
						 | 
					@ -68,5 +70,13 @@ define({
 | 
				
			||||||
		already_in_room: 		'You are already in a room ({roomName}), stoopid.',
 | 
							already_in_room: 		'You are already in a room ({roomName}), stoopid.',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		unable_to_decrypt: 		'Unabled to decrypt received message, keys does not match.'
 | 
							unable_to_decrypt: 		'Unabled to decrypt received message, keys does not match.'
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						server: {
 | 
				
			||||||
 | 
							person_joined: 			'A person joined this room.',
 | 
				
			||||||
 | 
							person_left: 			'A person left this room.',
 | 
				
			||||||
 | 
							person_count: 			'There is {payload} person(s) in this room, including you.',
 | 
				
			||||||
 | 
							command_failed: 		'Server command failed, you\'re probably trying to du something bogus.',
 | 
				
			||||||
 | 
							bogus: 					'Received a bogus message from server.',
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
							
								
								
									
										23
									
								
								server.js
									
										
									
									
									
								
							
							
						
						
									
										23
									
								
								server.js
									
										
									
									
									
								
							| 
						 | 
					@ -12,19 +12,32 @@ app.io.route('room', {
 | 
				
			||||||
      req.socket.emit('room:generated',room);
 | 
					      req.socket.emit('room:generated',room);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    join: function(req) {
 | 
					    join: function(req) {
 | 
				
			||||||
      if(req.data) {
 | 
					      if( req.data ) {
 | 
				
			||||||
        req.socket.emit('room:joined',req.data);
 | 
					        req.socket.emit('room:joined',req.data);
 | 
				
			||||||
        req.socket.join(req.data);
 | 
					        req.socket.join(req.data);
 | 
				
			||||||
        req.socket.broadcast.to(req.data).emit('message:server', 'A person joined this room');
 | 
					        req.socket.broadcast.to(req.data).emit('message:server', {msg:'person_joined'} );
 | 
				
			||||||
        req.socket.current_room = req.data;
 | 
					        req.socket.current_room = req.data;
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        req.socket.emit('message:server', {msg:'command_failed'} );
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    leave: function(req) {
 | 
					    leave: function(req) {
 | 
				
			||||||
      if(req.data) {
 | 
					      if( req.data ) {
 | 
				
			||||||
        req.socket.emit('room:left');
 | 
					        req.socket.emit('room:left');
 | 
				
			||||||
        req.socket.leave(req.data);
 | 
					        req.socket.leave(req.data);
 | 
				
			||||||
        req.socket.broadcast.to(req.data).emit('message:server', 'A person left this room');
 | 
					        req.socket.broadcast.to(req.data).emit('message:server', {msg:'person_left'} );
 | 
				
			||||||
        req.socket.current_room = undefined;
 | 
					        req.socket.current_room = undefined;
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        req.socket.emit('message:server', {msg:'command_failed'} );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    count: function(req) {
 | 
				
			||||||
 | 
					      if( req.socket.current_room !== undefined ) {
 | 
				
			||||||
 | 
					        // This will fail on socket.io >= 1.0
 | 
				
			||||||
 | 
					        var client_count = app.io.sockets.clients(req.socket.current_room).length;
 | 
				
			||||||
 | 
					        req.socket.emit('message:server', {msg:'person_count', payload: client_count } );
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        req.socket.emit('message:server', {msg:'command_failed'} );
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					@ -40,7 +53,7 @@ app.io.sockets.on('connection', function(socket) {
 | 
				
			||||||
   socket.on('disconnect', function() {
 | 
					   socket.on('disconnect', function() {
 | 
				
			||||||
      // Notify other users of the room
 | 
					      // Notify other users of the room
 | 
				
			||||||
      if( socket.current_room !== undefined ) {
 | 
					      if( socket.current_room !== undefined ) {
 | 
				
			||||||
        socket.broadcast.to(socket.current_room).emit('message:server', 'A person left this room');
 | 
					        socket.broadcast.to(socket.current_room).emit('message:server', {msg:'person_left'} );
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
   });
 | 
					   });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue