Skip to content
Browse files

we are now successfully logging chat messages

  • Loading branch information...
1 parent 34676c1 commit a5d212f9f950074eae533dac806939e99c6f1de2 @Aaronontheweb Aaronontheweb committed Apr 14, 2012
Showing with 28 additions and 7 deletions.
  1. +3 −3 lib/models/chatlog.js
  2. +25 −4 lib/server.js
View
6 lib/models/chatlog.js
@@ -11,10 +11,10 @@ var tableName = 'messages';
var tableQuery = azure.TableQuery;
chatLog = function(storageAccount, storageKey){
- this.tableClient = azure.createTableService(storageAccount, storageKey);
+ //this.tableClient = azure.createTableService(storageAccount, storageKey);
//In case we feel like working with the storage emulator
-/* this.tableClient = azure.createTableService(ServiceClient.DEVSTORE_STORAGE_ACCOUNT,
- ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY, ServiceClient.DEVSTORE_TABLE_HOST);*/
+ this.tableClient = azure.createTableService(ServiceClient.DEVSTORE_STORAGE_ACCOUNT,
+ ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY, ServiceClient.DEVSTORE_TABLE_HOST);
};
/* Initialization method to ensure that our Azure Table exists */
View
29 lib/server.js
@@ -6,9 +6,11 @@
var express = require('express')
, sessions = require('cookie-sessions')
, mapper = require('./helpers/mapper')
+ , messageLog = require('./models/chatlog').chatLog;
var app = module.exports = express.createServer();
-
+var chatLog = new messageLog();
+chatLog.init();
var io = require('socket.io').listen(app);
//Dynamic view helpers
@@ -59,26 +61,45 @@ io.configure(function () {
var usernames = [];
+var logMessage = function(message){
+ chatLog.save('main', message, function(error, savedMessage){
+ if(error){ //If for some reason we were unable to save the chat message
+ console.log(error.message);
+ }
+ });
+}
+
/* socket-io magic */
io.sockets.on('connection', function (socket) {
socket.on('sendchat', function (data) {
- io.sockets.emit('updatechat', mapper.createChatMessage(socket.username, data));
+ var message = mapper.createChatMessage(socket.username, data);
+
+ io.sockets.emit('updatechat', message);
+
+ logMessage(message);
});
socket.on('newuser', function(username){
socket.username = username;
usernames[username] = username;
socket.emit('updatechat', mapper.createChatMessage('SERVER', 'you have connected'));
- socket.broadcast.emit('updatechat', mapper.createChatMessage('SERVER', username + ' has connected'));
+
+ var message = mapper.createChatMessage('SERVER', username + ' has connected');
+ socket.broadcast.emit('updatechat', message);
+ logMessage(message);
+
socket.emit('loadusers', mapper.mapArray(usernames, mapper.createUser));
socket.broadcast.emit('adduser', mapper.createUser(username));
});
socket.on('disconnect', function(){
delete usernames[socket.username];
io.sockets.emit('removeuser', mapper.createUser(socket.username));
- socket.broadcast.emit('updatechat', mapper.createChatMessage('SERVER', socket.username + ' has disconnected'));
+ var message = mapper.createChatMessage('SERVER', socket.username + ' has disconnected')
+ socket.broadcast.emit('updatechat', message);
+
+ logMessage(message);
});
});

0 comments on commit a5d212f

Please sign in to comment.
Something went wrong with that request. Please try again.