Permalink
Browse files

Minor update: Missing sessionIds are now recreated (useful during dev…

…elopment), new projects now use Nib instead of helpers.styl, Jade formatting errors inside client-side templates are now caught
  • Loading branch information...
1 parent c3ec58d commit 5b2a6e3979287877afb5a0fc565d056f4f84dabd Owen Barnes committed Apr 24, 2012
@@ -14,7 +14,7 @@ First install `mocha` and `should.js`:
Create a directory for your tests:
- mkdir test
+ mkdir test
For this example we're going to test the `ss.rpc('app.square')` function shown below:
@@ -38,7 +38,7 @@ Note the `responderId` is sent before the pipe (`|`) character.
Request Responders are simply regular Node.js modules which you can easily create and publish on NPM. To help you get started quickly, we've made an ultra-simple example called `echo` which you can clone from Github and extend as you wish:
- git clone https://github.com/socketstream/ss-echo-responder.git
+ git clone https://github.com/socketstream/ss-echo-responder.git
### Adding a new responder to your app
View
@@ -70,8 +70,6 @@ exports.generate = function(program) {
cp("/server/rpc/demo." + codeExtension);
cp('/client/css/libs/reset.css');
cp("/client/css/app.demo.styl", "/client/css/app.styl");
- cp('/client/css/demo.styl');
- cp('/client/css/helpers.styl');
mkdir('/client/templates/chat');
cp("/client/templates/chat/message." + viewExtension);
cp("/client/views/app.demo." + viewExtension, "/client/views/app." + viewExtension);
@@ -1,5 +1,7 @@
var client, defaultEngine, formatters, pathlib, selectEngine, suggestedId, templateEngines, wrapTemplate;
+require('colors');
+
pathlib = require('path');
formatters = require('./formatters');
@@ -56,15 +58,21 @@ exports.generate = function(dir, files, cb) {
formatter = engine.selectFormatter(path, formatters.byExtension, formatter);
}
formatter || (formatter = formatters.byExtension['html']);
- return formatter.compile(fullPath, {}, function(output) {
- templates.push(wrapTemplate(output, path, fullPath, engine, prevEngine));
- prevEngine = engine;
- if (templates.length === files.length) {
- output = templates.join('');
- if (engine !== null && engine.suffix) output += engine.suffix();
- return cb(output);
- }
- });
+ try {
+ return formatter.compile(fullPath, {}, function(output) {
+ templates.push(wrapTemplate(output, path, fullPath, engine, prevEngine));
+ prevEngine = engine;
+ if (templates.length === files.length) {
+ output = templates.join('');
+ if (engine !== null && engine.suffix) output += engine.suffix();
+ return cb(output);
+ }
+ });
+ } catch (e) {
+ console.log(("! Errror formatting " + formatter.name + " template").red);
+ console.error(e.message);
+ return cb('');
+ }
});
};
@@ -51,7 +51,7 @@ module.exports = function(responderId, config, ss) {
message: message
}
};
- ss.log(''.red, msgLogName, req.method, e.message.red);
+ ss.log(''.red, msgLogName, req.method, e.message.red);
return send(JSON.stringify(obj));
}
},
@@ -36,9 +36,6 @@ module.exports = function(ss, middleware) {
if (typeof file.actions !== 'function') {
throw new Error("'exports.actions' function for '" + req.method + "' must be a function");
}
- if (file.before) {
- throw new Error("Important! The RPC middleware API changed in 0.3 alpha3. Please see https://github.com/socketstream/socketstream/blob/master/HISTORY.md");
- }
cb = function() {
var args;
args = Array.prototype.slice.call(arguments);
View
@@ -1,4 +1,4 @@
-var channels, connect, sessionStore, subscriptions;
+var appendMethods, channels, connect, create, sessionStore, subscriptions;
connect = require('connect');
@@ -23,41 +23,51 @@ exports.store = {
};
exports.create = function() {
- var Session, sessionID, thisSession;
- Session = connect.session.Session;
- sessionID = connect.utils.uid(24);
- thisSession = new Session({
- sessionID: sessionID,
- sessionStore: sessionStore
- });
- thisSession.cookie = {
- maxAge: null
- };
- thisSession.save();
- return sessionID;
+ var sessionId;
+ sessionId = connect.utils.uid(24);
+ create(sessionId);
+ return sessionId;
};
exports.find = function(sessionId, socketId, cb) {
return sessionStore.load(sessionId, function(err, session) {
- if (!session) return cb(false);
- session.channel = channels(session, socketId);
- session.setUserId = function(userId, cb) {
- if (cb == null) cb = function() {};
- this.userId = userId;
- this._bindToSocket();
- return this.save(cb);
- };
- session._bindToSocket = function() {
- if (session.userId != null) subscriptions.user.add(session.userId, socketId);
- if ((session.channels != null) && session.channels.length > 0) {
- session.channel._bindToSocket();
- }
- return this;
- };
- session.save = function(cb) {
- return sessionStore.set(sessionId, session, cb);
- };
- session._bindToSocket();
- return cb(session);
+ if (!session) session = create(sessionId);
+ return appendMethods(session, socketId, cb);
});
};
+
+appendMethods = function(session, socketId, cb) {
+ session.channel = channels(session, socketId);
+ session.setUserId = function(userId, cb) {
+ if (cb == null) cb = function() {};
+ this.userId = userId;
+ this._bindToSocket();
+ return this.save(cb);
+ };
+ session._bindToSocket = function() {
+ if (session.userId != null) subscriptions.user.add(session.userId, socketId);
+ if ((session.channels != null) && session.channels.length > 0) {
+ session.channel._bindToSocket();
+ }
+ return this;
+ };
+ session.save = function(cb) {
+ return sessionStore.set(sessionId, session, cb);
+ };
+ session._bindToSocket();
+ return cb(session);
+};
+
+create = function(sessionId) {
+ var Session, session;
+ Session = connect.session.Session;
+ session = new Session({
+ sessionID: sessionId,
+ sessionStore: sessionStore
+ });
+ session.cookie = {
+ maxAge: options.maxAge
+ };
+ session.save();
+ return session;
+};
@@ -1,4 +1,4 @@
-# This file gets called automatically by SocketStream and must always exist
+# This file automatically gets called first by SocketStream and must always exist
# Make 'ss' available to all modules and the browser console
window.ss = require('socketstream')
@@ -1,4 +1,4 @@
-// This file gets called automatically by SocketStream and must always exist
+// This file automatically gets called first by SocketStream and must always exist
// Make 'ss' available to all modules and the browser console
window.ss = require('socketstream');
@@ -1,7 +1,10 @@
// Example Stylus file
+//
+// Stylus docs: http://learnboost.github.com/stylus/docs/js.html
+// Nib docs: http://visionmedia.github.com/nib
+
+@import 'nib'
-@import 'helpers'
-@import 'demo'
// Main
@@ -10,7 +13,7 @@ body, html
body
font normal 1em sans-serif
- vertical-gradient #eee, #fff
+ background linear-gradient(top, #eee, #fff)
text-align center
p
@@ -29,3 +32,47 @@ h1
#content
padding 50px
+
+// Quick Chat Demo
+
+#demo
+ border 1px solid #ccc
+ width 700px
+ text-align left
+ margin 50px auto
+ padding 15px
+ border-radius 5px
+ h3
+ font-size 1.0em
+ font-weight normal
+ h5
+ padding 5px 0
+ color #666
+ font-weight normal
+ font-size 0.8em
+ #myMessage, #chatlog
+ border none
+ box-shadow inset 0 0 2px #777
+ border-radius 5px
+ #myMessage
+ width 690px
+ padding 5px
+ #chatlog
+ width 700px
+ background-color white
+ margin 5px 0
+ p
+ font-size 0.9em
+ color #000a68
+ padding 3px 9px
+ margin 0
+ p:first-child
+ padding-top 9px
+ p:last-child
+ padding-bottom 9px
+ span.time
+ color #666
+ float right
+ width 70px
+ text-align right
+ font-size 0.7em
@@ -1,46 +0,0 @@
-// Quick Chat Demo
-
-@import 'helpers'
-
-#demo
- border 1px solid #ccc
- width 700px
- text-align left
- margin 50px auto
- padding 15px
- round 5px
- h3
- font-size 1.0em
- font-weight normal
- h5
- padding 5px 0
- color #666
- font-weight normal
- font-size 0.8em
- #myMessage, #chatlog
- border none
- box-shadow inset 0 0 2px #777
- round 5px
- #myMessage
- width 690px
- padding 5px
- #chatlog
- width 700px
- background-color white
- margin 5px 0
- p
- font-size 0.9em
- color #000a68
- padding 3px 9px
- margin 0
- p:first-child
- padding-top 9px
- p:last-child
- padding-bottom 9px
- span.time
- color #666
- float right
- width 70px
- text-align right
- font-size 0.7em
-
@@ -1,25 +0,0 @@
-// Helper functions
-
-vendor(prop, args)
- -webkit-{prop} args
- -moz-{prop} args
- {prop} args
-
-round(n)
- vendor border-radius, n
-
-round-corner(tb, lr, n)
- border-{tb}\-{lr}-radius n
- -moz-border-radius-{tb}{lr} n
-
-box-shadow()
- vendor box-shadow, arguments
-
-opacity(n)
- opacity n
- -moz-opacity n
-
-vertical-gradient(from, to)
- background-color to
- background-image -webkit-gradient(linear, left top, left bottom, from(from), to(to))
- background-image -moz-linear-gradient(top, from, to)
@@ -10,6 +10,7 @@ exports.actions = function(req, res, ss) {
//req.use('example.authenticated')
return {
+
sendMessage: function(message) {
if (message && message.length > 0) { // Check for blank messages
ss.publish.all('newMessage', message); // Broadcast the message to everyone
@@ -18,5 +19,7 @@ exports.actions = function(req, res, ss) {
return res(false);
}
}
+
};
+
};
View
@@ -77,8 +77,6 @@ exports.generate = (program) ->
cp("/server/rpc/demo.#{codeExtension}")
cp('/client/css/libs/reset.css')
cp("/client/css/app.demo.styl", "/client/css/app.styl")
- cp('/client/css/demo.styl')
- cp('/client/css/helpers.styl')
mkdir('/client/templates/chat')
cp("/client/templates/chat/message.#{viewExtension}")
cp("/client/views/app.demo.#{viewExtension}", "/client/views/app.#{viewExtension}")
@@ -6,6 +6,7 @@
# You may combine several types of template engines together - very useful when converting a site from one format
# to another, or experimenting with different template engines
+require('colors')
pathlib = require('path')
formatters = require('./formatters')
client = require('./system')
@@ -68,15 +69,20 @@ exports.generate = (dir, files, cb) ->
formatter ||= formatters.byExtension['html']
# Use the formatter to pre-process the template before passing it to the engine
- formatter.compile fullPath, {}, (output) ->
- templates.push(wrapTemplate(output, path, fullPath, engine, prevEngine))
- prevEngine = engine
-
- # Return if last template
- if templates.length == files.length
- output = templates.join('')
- output += engine.suffix() if engine != null and engine.suffix
- cb(output)
+ try
+ formatter.compile fullPath, {}, (output) ->
+ templates.push(wrapTemplate(output, path, fullPath, engine, prevEngine))
+ prevEngine = engine
+
+ # Return if last template
+ if templates.length == files.length
+ output = templates.join('')
+ output += engine.suffix() if engine != null and engine.suffix
+ cb(output)
+ catch e
+ console.log("! Errror formatting #{formatter.name} template".red)
+ console.error(e.message)
+ cb('')
# PRIVATE
Oops, something went wrong.

0 comments on commit 5b2a6e3

Please sign in to comment.