Skip to content
Browse files

initial

  • Loading branch information...
0 parents commit 7d63aa3087e9fd3fd97d0d9ad863b2a941950354 @dominictarr committed Jun 1, 2012
Showing with 117 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +13 −0 client.js
  3. +83 −0 index.js
  4. +18 −0 package.json
  5. 0 readme.markdown
3 .gitignore
@@ -0,0 +1,3 @@
+node_modules
+node_modules/*
+npm_debug.log
13 client.js
@@ -0,0 +1,13 @@
+
+var reconnector = require('reconnector')
+
+module.exports = function (create) {
+
+ if(!create)
+ create = window.SKATES.create
+
+ var emitter = reconnector(create)
+ emitter.options = SKATES
+ return emitter
+//what about middle ware for the client?
+}
83 index.js
@@ -0,0 +1,83 @@
+/*
+ I keep forgetting how to
+ setup websocket apps.
+*/
+
+function defaults (obj, defaults) {
+ obj = obj || {}
+ for(var k in defaults)
+ obj[k] = obj[k] || defaults[k]
+ return obj
+}
+
+/*
+ HMM, it seems that ws are allowed to do cros-origin!
+ probably the best architecture then,
+ is to come in through a load balancer to load the app,
+ but then have smart clients, that communicate directly to the correct server via websockets.
+ or, jsonp longpolling or whatever.
+
+ this will require a lot more brains in the clients,
+ but that is what I'm all about.
+*/
+
+var path = require('path')
+var fs = require('fs')
+var sockjs = require('sockjs')
+var connect = require('connect')
+var browserify = require('browserify')
+var toEmitter = require('reconnector/ws-server')
+
+function createServer(opts) {
+ opts = defaults(opts, {
+ url: '/skates',
+ middleware: connect.createServer(),
+ entry: './client.js',
+ cache: true
+ })
+ var sox = sockjs.createServer()
+ var app = opts.middleware
+ var _listen = app.listen
+ //TODO... automatically insert browserify and sock.
+ app.listen = function () {
+ var args = [].slice.call(arguments)
+ sox.installHandlers(_listen.apply(app, args), {
+ prefix: opts.prefix || opts.url
+ })
+ return app
+ }
+ var browser = browserify(opts)
+ browser.addEntry(opts.entry)
+
+ //this adds some global information from the server.
+ //it will be the same for all clients that have loaded data
+ //from this server
+
+ browser.prepend([
+ 'window.RECONNECTOR = ' + JSON.stringify({
+ timestamp: app.timestamp = Date.now(), //this is the version of the code you are on
+ url: opts.url
+ })
+ //it will be useful to pass initialization data on the connection, too.
+ ,
+ 'window.RECONNECTOR.create = ' + function () {
+ return new SockJS(RECONNECTOR.url)
+ }.toString() + ';'
+ ].join('\n'))
+ browser.prepend(fs.readFileSync('../src/sockjs-0.3.min.js'))
+/*
+ REMEMBER to make sure that the javascript gets served.
+ maybe inject it into the page?
+ or is that too overbearing?
+*/
+ app.use(browser)
+ sox.on('connection', function () {
+ var args = [].slice.call(arguments)
+ args[0] = toEmitter(args[0])
+ args.unshift('connection')
+ app.emit.apply(app, args)
+ })
+ return app
+}
+
+
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "skates",
+ "version": "0.0.0",
+ "description": "streamline creating WebSocket apps",
+ "homepage": "http://github.com/dominictarr/skates",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/dominictarr/skates.git"
+ },
+ "dependencies": {},
+ "devDependencies": {},
+ "author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)",
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+ "browserify": "./client.js"
+}
0 readme.markdown
No changes.

0 comments on commit 7d63aa3

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