Permalink
Browse files

Changing API to be better

  • Loading branch information...
1 parent a6b01b9 commit f4315027bf17cefe99d11764e19dc51544810659 @saikat committed Sep 2, 2010
Showing with 53 additions and 53 deletions.
  1. +9 −9 README.md
  2. +1 −1 example/client/AppController.j
  3. +3 −4 example/dashboard/AppController.j
  4. +6 −8 example/server.js
  5. +34 −31 lib/socket-logger.js
View
@@ -14,27 +14,27 @@ How to use
You will need to install [Socket.IO-node](http://github.com/learnboost/socket.io-node) and have a socket-io server running. Then, clone this repo. Now put this in your server and smoke it:
+
var http = require('http'),
- log = require('../lib/socket-logger').defaultLogger,
+ logger = require('../lib/socket-logger').defaultLogger,
io = require('./socket.io'),
-
server = http.createServer(function(req, res){
});
-
+
server.listen(8080);
- log.authToken = 'my_secret_token_for_the_dashboard_client';
+
var socket = io.listen(server);
socket.on('connection', function(client){
- log.listen('message', client);
- log.listen('disconnect', client);
- client.on('message', function(message) {
+ client.on('message', function(message) {
});
- client.on('disconnect', function() {
+ client.on('disconnect', function() {
});
});
- log.listen('connection', socket);
+ logger.logLevel = 2;
+ logger.authToken = 'my_secret_token_for_the_dashboard_client';
+ logger.monitor(socket);
Running the demo
================
@@ -20,7 +20,7 @@
- (void)keyDown:(CPEvent)anEvent
{
[super keyDown:anEvent];
- [theSocket sendMessage:{'c' : [anEvent characters]}];
+ [theSocket sendMessage:[anEvent characters]];
}
@end
@@ -32,7 +32,7 @@
- (void)socketDidConnect:(SCSocket)aSocket
{
lps = [0];
- [theSocket sendMessage:{'authToken' : 'my_secret_token_for_the_dashboard_client'}];
+ [theSocket sendMessage:'my_secret_token_for_the_dashboard_client'];
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
label1 = [CPTextField labelWithTitle:"Breakdown"];
@@ -91,9 +91,8 @@
- (void)socket:(SCSocket)aSocket didReceiveMessage:(CPString)aMessage
{
- if (aMessage.body && aMessage.body.c)
- {
- var theLetter = aMessage.body.c;
+ if (aMessage[3] && aMessage[3].length == 1) {
+ var theLetter = aMessage[3];
var theIndex = theLetter.charCodeAt(0) - 'a'.charCodeAt(0);
values[theIndex]++;
[barChart reloadData];
View
@@ -1,23 +1,21 @@
var http = require('http'),
-log = require('../lib/socket-logger').defaultLogger,
+logger = require('../lib/socket-logger').defaultLogger,
io = require('./socket.io'),
-
server = http.createServer(function(req, res){
});
-log.logLevel = 1;
-
server.listen(8080);
-log.authToken = 'my_secret_token_for_the_dashboard_client';
+
var socket = io.listen(server);
socket.on('connection', function(client){
- log.listen('message', client);
- log.listen('disconnect', client);
client.on('message', function(message) {
});
client.on('disconnect', function() {
});
});
-log.listen('connection', socket);
+logger.logLevel = 2;
+logger.failSilently = false;
+logger.authToken = 'my_secret_token_for_the_dashboard_client';
+logger.monitor(socket);
View
@@ -5,44 +5,47 @@ function makeLogger() {
authToken : null,
logLevel : 0,
failSilently : true,
- // This whole method is hacky, but I'm not entirely sure what I want
- // the API to be
- listen : function(event, obj) {
- obj.on(event, function() {
- try {
- var currentTime = new Date(),
- timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
- var theMsg = {'time' : timestamp, 'action' : event};
- // Socket event, the argument was a Client
- if (arguments && arguments[0] && arguments[0].hasOwnProperty('sessionId')) {
- theMsg.client = arguments[0].sessionId;
- // Possible message event
+ monitor : function(socket) {
+ socket.on('connection', function(client) {
+ logger.log(['connect', client.sessionId]);
+ client.on('message', function(message) {
+ try {
+ var theMessage = ['message'];
+ if (message === logger.authToken)
+ logger.socketLogger = client;
+ if (logger.logLevel > 0)
+ theMessage.push(client.sessionId);
+ if (logger.logLevel > 1) {
+ try { var parsedMsg = JSON.parse(message); } catch (ex) { }
+ theMessage.push(parsedMsg ? parsedMsg : message);
+ }
+ logger.log(theMessage);
+ } catch (ex) {
+ if (!logger.failSilently)
+ throw ex;
}
- else if (arguments && arguments[0]) {
- try { var parsedMsg = JSON.parse(arguments[0]); } catch (x) {}
- if (parsedMsg && logger.logLevel > 0)
- theMsg.body = parsedMsg;
- if (obj.hasOwnProperty('sessionId'))
- theMsg.client = obj.sessionId;
-
- if (parsedMsg && parsedMsg.authToken === logger.authToken)
- logger.socketLogger = obj;
- }
- logger.log(theMsg);
- } catch (ex) {
- if (!logger.failSilently)
- throw ex;
- }
+ });
+ client.on('disconnect', function() {
+ logger.log(['disconnect', client.sessionId]);
+ });
});
},
- log : function(msg, logOptions) {
+ // Expects a JSON-parsable object
+ log : function(msg) {
try {
- var serializedMsg = JSON.stringify(msg);
+ var currentTime = new Date(),
+ timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
+ if (msg.unshift)
+ msg.unshift(timestamp);
+ else
+ msg.timestamp = timestamp;
+ if (typeof(msg) === "object")
+ msg = JSON.stringify(msg);
stream = logger.stream;
- stream.write(serializedMsg + '\n', 'utf8');
+ stream.write(msg + '\n', 'utf8');
if (logger.socketLogger && logger.socketLogger.send) {
- logger.socketLogger.send(serializedMsg);
+ logger.socketLogger.send(msg);
}
} catch (ex) {
if (!logger.failSilently)

0 comments on commit f431502

Please sign in to comment.