Skip to content

Commit

Permalink
added engine.io for websocket communication and a small demo with it
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Alabor committed Sep 1, 2013
1 parent 7f0379e commit ef1327d
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 72 deletions.
5 changes: 3 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** File: .jshintrc
* Configuration for JSHint, the easier JSLint alternative.
*
* Since the client part uses AngularJS, we allow the "angular" object as a
* global.
* Since the client part uses AngularJS and Engine.IO, we allow the "angular"
* and "eio" object as globals.
*
* See also:
* - http://www.jshint.com/docs/#config
Expand All @@ -24,5 +24,6 @@

, "globals": {
"angular": true
, "eio": true
}
}
6 changes: 1 addition & 5 deletions config/config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
*
* Server:
* Port:
* On which network port should the KaffeeUndKuchen server be listening fot
* On which network port should the KaffeeUndKuchen server be listening for
* connections?
* announceWithBonjour:
* Should the KaffeeUndKuchen server be announced using Bonjour in the local
* network?
*
* Spotify Settings:
* This section contains all stuff related to Spotify. Login to spotify.com and
Expand All @@ -26,7 +23,6 @@
var config = {
server: {
port: 80
, announceWithBonjour: true
}
, spotify: {
username: 'username'
Expand Down
76 changes: 38 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "kaffeeundkuchen",
"version": "0.0.1",
"description": "kaffeeundkuchen - coffee and pi: Rock your party with this crowd driven spotify player.",
"keywords": [
"raspberrypi",
"pi",
"raspberry",
"airtunes",
"airplay",
"spotify",
"sound",
"audio",
"music",
"party"
],
"main": "app.js",
"repository": "http://github.com/swissmanu/kaffeeundkuchen",
"author": "Manuel Alabor",
"license": "MIT",
"dependencies": {
"airtunes": "~0.1.3",
"optimist": "~0.3.5",
"express": "~3.0.6",
"libspotify": "~0.1.0",
"igneous": "0.1.6",
"debug": "0.7.2"
},
"devDependencies": {
"mocha": "~1.8.2",
"chai": "~1.5.0",
"jshint": "~1.1.0",
"mocha-lcov-reporter": "~0.0.1",
"coveralls": "~2.0.7"
},
"scripts": {
"start": "node src/app.js",
"test": "make test"
}
"name": "kaffeeundkuchen",
"version": "0.0.1",
"description": "kaffeeundkuchen - coffee and pi: Rock your party with this crowd driven spotify player.",
"keywords": [
"raspberrypi",
"pi",
"raspberry",
"airtunes",
"airplay",
"spotify",
"sound",
"audio",
"music",
"party"
],
"main": "app.js",
"repository": "http://github.com/swissmanu/kaffeeundkuchen",
"author": "Manuel Alabor",
"license": "MIT",
"dependencies": {
"airtunes": "~0.1.3",
"express": "~3.0.6",
"libspotify": "~0.1.0",
"debug": "0.7.2",
"engine.io": "0.7.9",
"engine.io-client": "0.7.9"
},
"devDependencies": {
"mocha": "~1.8.2",
"chai": "~1.5.0",
"jshint": "~1.1.0",
"mocha-lcov-reporter": "~0.0.1",
"coveralls": "~2.0.7"
},
"scripts": {
"start": "node src/app.js",
"test": "make test"
}
}
34 changes: 7 additions & 27 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@

var debug = require('debug')('kaffeeundkuchen')
, express = require('express')
, config = require('../config/config')
, Playlist = require('./models/playlist')
, SpotifyWrapper = require('./utils').SpotifyWrapper

, playlist = new Playlist()
, spotifyWrapper = new SpotifyWrapper(config);


function createExpressApp(config, spotifyWrapper, playlist) {
debug('create express app');

var app = express()
, api = require('./api')()
, client = require('./client')();

app.set('config', config);
app.set('spotifyWrapper', spotifyWrapper);
app.set('playlist', playlist);

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/client/public'));

app.use(api);
app.use(client);

return app;
}
, spotifyWrapper = new SpotifyWrapper(config)

var app = createExpressApp(config, spotifyWrapper, playlist);
, webSocket = require('./websocket')
, appFactory = require('./factory')
, app = appFactory(config, spotifyWrapper, playlist)
, httpServer;

debug('Starting KaffeeUndKuchen on port ' + config.server.port);
app.listen(config.server.port);
httpServer = app.listen(config.server.port);
webSocket(httpServer);
11 changes: 11 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ function handlePartial(req, res) {
res.sendfile(join(__dirname, 'views', 'partials', name));
}

function handleEngineIOClient(req, res) {
debug('handle engine.io client request');

var jsPath = join(
__dirname, '..', '..', 'node_modules'
, 'engine.io-client', 'engine.io.js');

res.sendfile(jsPath);
}

function clientInit() {
debug('inititialize client module');

var clientApp = require('express')();

clientApp.get('/', handleIndex);
clientApp.get('/partials/:name', handlePartial);
clientApp.get('/app/vendor/engine.io.js', handleEngineIOClient);

// The AngularJS app uses the HTML 5 mode for building URLs. So it's safe to
// handle all other requests like an index request.
Expand Down
10 changes: 10 additions & 0 deletions src/client/public/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@

})();

var socket = new eio.Socket('ws://localhost:8080/');
socket.on('open', function() {
socket.on('message', function(data) {
console.log('got message! ' + data);
socket.send(data); // echo
});
socket.on('close', function() {
console.log('socket closed!');
});
});
1 change: 1 addition & 0 deletions src/client/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">

<script src="app/vendor/engine.io.js"></script>
<script src="app/vendor/angular/angular.js"></script>
<script src="app/app.js"></script>
<script src="app/controllers.js"></script>
Expand Down
25 changes: 25 additions & 0 deletions src/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var debug = require('debug')('kaffeeundkuchen.factory')
, express = require('express');

function createExpressApp(config, spotifyWrapper, playlist) {
debug('create express app');

var app = express()
, api = require('./api')()
, client = require('./client')();

app.set('config', config);
app.set('spotifyWrapper', spotifyWrapper);
app.set('playlist', playlist);

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/client/public'));

app.use(api);
app.use(client);

return app;
}

module.exports = createExpressApp;
36 changes: 36 additions & 0 deletions src/websocket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var debug = require('debug')('kaffeeundkuchen.websocket')
, engine = require('engine.io');


function handleMessage(data) {
debug('received websocket message');
console.log(data);
}

function handleClose() {
debug('closed websocket connection');
}

function handleNewConnection(socket) {
debug('new websocket connection');
socket.on('message', handleMessage);
socket.on('close', handleClose);

socket.send('hi there!');
}

/** Function: initWebSocket
* Attaches the Engine.IO WebSocket abstraction to any passed httpServer
* instance.
*
* Parameters:
* (http.Server) httpServer - Node.JS HTTP Server instance
*/
function initWebSocket(httpServer) {
debug('inititialize websocket module');

var websocketServer = engine.attach(httpServer);
websocketServer.on('connection', handleNewConnection);
}

module.exports = initWebSocket;

0 comments on commit ef1327d

Please sign in to comment.