Skip to content

Commit

Permalink
First working draft
Browse files Browse the repository at this point in the history
  • Loading branch information
zcei committed Apr 15, 2015
1 parent feadef4 commit 554110f
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.DS_Store
node_modules/
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
# remood.js

## How it works

#### You start by creating an app.

```js
var express = require('express'),
remood = require('remood'),
app = express(),
server = remood(app);

// Mount routes to your app
app.get('/', function(req, res) {
res.send('remood.js rocks!');
});

// BUT listen on the RETURNED server instance
server.listen(1337);
```

#### Then include frontend functionality

```html
<script src="js/remood.js"></script>
```

### Use it

#### Receiver

```js
// Initialize remood
var r = new remood();

// Register remood events
r.on('yourEventId', function(msg) {
console.log(msg);
});
```

#### Remote

```js
// Initialize remood as remote
var r = new remood({ remote: true });

r.send({
id: 'yourEventId',
type: eventName, // Most of the time 'click' or 'input'
data: 'my payload'
});
```

#### jQuery

Or use build in jQuery connector

```html
<div id="play">Click me!</div>
```

```js
$('#play').connect('click', function() {
// Callback after payload {
// id: 'play',
// type: 'click',
// data: ''
// } has been sent

console.log('#play has been clicked');
});
```

## Development

In case you need to have the latest versions of dependencies:

```sh
$ cd node_modules/remood/
$ npm update --dev --depth 0
$ node ./node_modules/gulp/bin/gulp.js assets
```
14 changes: 13 additions & 1 deletion assets/js/remood.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
var jQuery = require('jquery'),
io = require('socket.io-client'),
_ = {
each: require('amp-each'),
pluck: require('amp-pluck'),
select: require('amp-filter')
};

if (window && window.$) {
jQuery = window.$;
}

(function($, io, _) {

var socket = io();
Expand Down Expand Up @@ -94,4 +106,4 @@
});
}
});
})(window.jQuery, window.io, window._);
})(jQuery, io, _);
25 changes: 25 additions & 0 deletions assets/js/vendor/10d45h.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added dist/.keep
Empty file.
38 changes: 38 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var del = require('del');

gulp.task('assets:js', function () {
// set up the browserify instance on a task basis
var b = browserify({
entries: './assets/js/remood.js',
debug: true
});

return b.bundle()
.pipe(source('./assets/js/remood.js'))
.pipe(buffer())
//.pipe(sourcemaps.init({loadMaps: true}))
// Add transformation tasks to the pipeline here.
// .pipe(uglify())
.on('error', gutil.log)
//.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
});

gulp.task('clean', function(done) {
del(['./dist/*', '!./dist/.keep'], done);
});

gulp.task('default',
['assets']
);

gulp.task('assets',
['clean', 'assets:js']
);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/app');
50 changes: 50 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var express = require('express'),
http = require('http'),
socketIO = require('socket.io'),
RemoodConnection = require('./remood.connection');

module.exports = function(app, options) {
var server = http.createServer(app),
io = socketIO(server);

app.use(express.static(__dirname + '/../dist/assets'));

app.use(function noop(req, res, next) {
next();
});

io.on('connection', function(socket) {

console.log('Incomming connection, shaking hands:');

// Handshake
socket.on('remood-auth', function(msg) {

var createConnection = function(id) {
var con = new RemoodConnection(socket, msg.type, id);
console.log('+ Created new connection with id:', con.id());
socket.emit('remood-auth', { id: con.id() });
};

console.log('+ Socket is a ' + msg.type);

if (msg.id) {
con = RemoodConnection.find(msg.id);
if (con) {
con.setSocketForType(socket, msg.type);
console.log('+ Successfully connected to connection with id:', msg.id);
console.log(con.statusString(true));
} else {
console.log('- No conection found for given id, creating new one');
createConnection(msg.id);
}
} else {
createConnection();
}
});

socket.emit('remood-auth');
});

return server;
};
4 changes: 2 additions & 2 deletions lib/remood.connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var randomstring = require('randomstring'),
pluralize = require('pluralize'),
connections = [],
_ = require('lodash');
_ = require('lodash'),
connections = [];

RemoodConnection = function(socket, type, identifier) {

Expand Down
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "remood.js",
"version": "0.1.0",
"description": "Add remote-control features to your webapp",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/h-w-z/remood.js.git"
},
"keywords": [
"socket"
],
"author": "HWZ",
"license": "MIT",
"bugs": {
"url": "https://github.com/h-w-z/remood.js/issues"
},
"homepage": "https://github.com/h-w-z/remood.js",
"dependencies": {
"express": "~4.12.3",
"lodash": "~3.6.0",
"pluralize": "~1.1.2",
"randomstring": "~1.0.5",
"socket.io": "~1.3.5"
},
"devDependencies": {
"amp-each": "~1.0.1",
"amp-filter": "~1.0.1",
"amp-pluck": "~1.0.0",
"browserify": "~9.0.8",
"del": "~1.1.1",
"gulp": "~3.8.11",
"gulp-sourcemaps": "~1.5.1",
"gulp-uglify": "~1.2.0",
"gulp-util": "~3.0.4",
"jquery": "~2.1.3",
"socket.io-client": "~1.3.5",
"vinyl-buffer": "~1.0.0",
"vinyl-source-stream": "~1.1.0"
}
}

0 comments on commit 554110f

Please sign in to comment.