Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"ng-tags-input": "2.0.1",
"angular-notify": "~1.1.0",
"angular-loading-bar": "~0.5.0",
"amcharts": "~3.10.0"
"amcharts": "~3.10.0",
"angular-socket-io": "~0.6.0"
},
"resolutions": {
"angular": "~1.2.20"
Expand Down
2 changes: 2 additions & 0 deletions lib/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ getVersionFromGithub = (callback)->
exports.attach = (app)->

app.get '/ping', (req, res)-> res.send('pong!')
app.get('socket').on 'connection', (socket)->
console.log('>>>', socket)

app.get '/whereAmI', (req, res)->
res.json {
Expand Down
6 changes: 5 additions & 1 deletion lib/script/controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ exports.callScript = (sid, arg=[], options)->



exports.runScript = (id, arg=[], options={}, user, notes='')->
exports.runScript = (id, arg=[], options={}, user, notes='', io_runlog)->

uid = user._id
options.shell = options.shell or 'sh'
Expand Down Expand Up @@ -254,8 +254,12 @@ exports.runScript = (id, arg=[], options={}, user, notes='')->
# save to mongodb
scriptLogs.content = shellOutput
# scriptLogs.endAt = new Date()

#TODO: disable write to db instantly
scriptLogs.save() #! Async problem?

io_runlog.emit(scriptLogs._id + '-output', shellOutput)

exc.stdout.on 'data', receiveOutput
exc.stderr.on 'data', receiveOutput
exc.on 'close', (code)->
Expand Down
5 changes: 4 additions & 1 deletion lib/script/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ path = require 'path'

exports.route = (app)->

io = app.get('socket')
io_runlog = io.of('/runlog')

app.post '/scripts/create', (req, res)->
Ctrl
.addScript(req.body, req.cookies.uid)
Expand Down Expand Up @@ -132,7 +135,7 @@ exports.route = (app)->
notes = req.body.notes

Ctrl
.runScript sid, args, app.get('configs').spawnOptions, req.session.user, notes
.runScript sid, args, app.get('configs').spawnOptions, req.session.user, notes, io_runlog
.then (cb, result, doc, logs)->
res.json {
success: true,
Expand Down
11 changes: 10 additions & 1 deletion lib/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ path = require 'path'
router = require './router'
utils = require './utils'

socketIO = require 'socket.io'
http = require 'http'

module.exports = (configs)->

# connect mongodb
utils.connectDB(configs.mongodb)

app = express()

# create socket.io service
server = http.Server(app)
io = socketIO(server)
app.set 'socket', io

app.use logger('dev')
app.set 'configs', configs

Expand Down Expand Up @@ -58,4 +67,4 @@ module.exports = (configs)->
# error: err
# }

return app
return server
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"pinyin": "~2.3.3",
"pm2": "~0.9.2",
"prompt": "~0.2.13",
"socket.io": "^1.1.0",
"thenjs": "~1.3.4"
},
"devDependencies": {
Expand Down
22 changes: 22 additions & 0 deletions public/components/angular-socket-io/.bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "angular-socket-io",
"version": "0.6.0",
"main": "socket.js",
"dependencies": {
"angular": "~1.2.6"
},
"devDependencies": {
"angular-mocks": "~1.2.6"
},
"homepage": "https://github.com/btford/angular-socket-io",
"_release": "0.6.0",
"_resolution": {
"type": "version",
"tag": "v0.6.0",
"commit": "15b1a96bd08ab33c02980f62a5fefcd3b3a7cc8b"
},
"_source": "git://github.com/btford/angular-socket-io.git",
"_target": "~0.6.0",
"_originalSource": "angular-socket-io",
"_direct": true
}
3 changes: 3 additions & 0 deletions public/components/angular-socket-io/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
node_modules/
bower_components/
8 changes: 8 additions & 0 deletions public/components/angular-socket-io/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- 0.10
before_script:
- npm install -g bower
- bower install
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
243 changes: 243 additions & 0 deletions public/components/angular-socket-io/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# angular-socket-io [![Build Status](https://travis-ci.org/btford/angular-socket-io.png)](https://travis-ci.org/btford/angular-socket-io)

Bower Component for using AngularJS with [Socket.IO](http://socket.io/),
based on [this](http://briantford.com/blog/angular-socket-io.html).


## Install

1. `bower install angular-socket-io` or [download the zip](https://github.com/btford/angular-socket-io/archive/master.zip).
2. Make sure the Socket.IO client lib is loaded. It's often served at `/socket.io/socket.io.js`.
3. Include the `socket.js` script provided by this component into your app.
4. Add `btford.socket-io` as a module dependency to your app.


## Usage

This module exposes a `socketFactory`, which is an API for instantiating
sockets that are integrated with Angular's digest cycle.


### Making a Socket Instance

```javascript
// in the top-level module of the app
angular.module('myApp', [
'btford.socket-io',
'myApp.MyCtrl'
]).
factory('mySocket', function (socketFactory) {
return socketFactory();
});
```

With that, you can inject your `mySocket` service into controllers and
other serivices within your application!

### Using Your Socket Instance

Building on the example above:

```javascript
// in the top-level module of the app
angular.module('myApp', [
'btford.socket-io',
'myApp.MyCtrl'
]).
factory('mySocket', function (socketFactory) {
return socketFactory();
}).
controller('MyCtrl', function (mySocket) {
// ...
});
```


## API

For the most part, this component works exactly like you would expect.
The only API addition is `socket.forward`, which makes it easier to add/remove listeners in a way that works with [AngularJS's scope](http://docs.angularjs.org/api/ng.$rootScope.Scope).

### `socket.on` / `socket.addListener`
Takes an event name and callback.
Works just like the method of the same name from Socket.IO.

### `socket.removeListener`
Takes an event name and callback.
Works just like the method of the same name from Socket.IO.

### `socket.removeAllListeners`
Takes an event name.
Works just like the method of the same name from Socket.IO.

### `socket.emit`
Sends a message to the server.
Optionally takes a callback.

Works just like the method of the same name from Socket.IO.

### `socket.forward`

`socket.forward` allows you to forward the events received by Socket.IO's socket to AngularJS's event system.
You can then listen to the event with `$scope.$on`.
By default, socket-forwarded events are namespaced with `socket:`.

The first argument is a string or array of strings listing the event names to be forwarded.
The second argument is optional, and is the scope on which the events are to be broadcast.
If an argument is not provided, it defaults to `$rootScope`.
As a reminder, broadcasted events are propagated down to descendant scopes.

#### Examples

An easy way to make socket error events available across your app:

```javascript
// in the top-level module of the app
angular.module('myApp', [
'btford.socket-io',
'myApp.MyCtrl'
]).
factory('mySocket', function (socketFactory) {
var mySocket = socketFactory();
mySocket.forward('error');
return mySocket;
});

// in one of your controllers
angular.module('myApp.MyCtrl', []).
controller('MyCtrl', function ($scope) {
$scope.$on('socket:error', function (ev, data) {

});
});
```

Avoid duplicating event handlers when a user navigates back and forth between routes:

```javascript
angular.module('myMod', ['btford.socket-io']).
controller('MyCtrl', function ($scope, socket) {
socket.forward('someEvent', $scope);
$scope.$on('socket:someEvent', function (ev, data) {
$scope.theData = data;
});
});
```


### `socketFactory({ ioSocket: }}`

This option allows you to provide the `socket` service with a `Socket.IO socket` object to be used internally.
This is useful if you want to connect on a different path, or need to hold a reference to the `Socket.IO socket` object for use elsewhere.

```javascript
angular.module('myApp', [
'btford.socket-io'
]).
factory('mySocket', function (socketFactory) {
var myIoSocket = io.connect('/some/path');

mySocket = socketFactory({
ioSocket: myIoSocket
});

return mySocket;
});
```

### `socketFactory({ scope: })`

This option allows you to set the scope on which `$broadcast` is forwarded to when using the `forward` method.
It defaults to `$rootScope`.


### `socketFactory({ prefix: })`

The default prefix is `socket:`.


#### Example

To remove the prefix:

```javascript
angular.module('myApp', [
'btford.socket-io'
]).
config(function (socketProvider) {
socketProvider.prefix('');
});
```



## Migrating from 0.2 to 0.3

`angular-socket-io` version `0.3` changes X to make fewer assumptions
about the lifecycle of the socket. Previously, the assumption was that your
application has a single socket created at config time. While this holds
for most apps I've seen, there's no reason you shouldn't be able to
lazily create sockets, or have multiple connections.

In `0.2`, `angular-socket-io` exposed a `socket` service. In `0.3`, it
instead exposes a `socketFactory` service which returns socket instances.
Thus, getting the old API is as simple as making your own `socket` service
with `socketFactory`. The examples below demonstrate how to do this.

### Simple Example

In most cases, adding the following to your app should suffice:

```javascript
// ...
factory('socket', function (socketFactory) {
return socketFactory();
});
// ...
```

### Example with Configuration

Before:

```javascript
angular.module('myApp', [
'btford.socket-io'
]).
config(function (socketProvider) {
socketProvider.prefix('foo~');
socketProvider.ioSocket(io.connect('/some/path'));
}).
controller('MyCtrl', function (socket) {
socket.on('foo~bar', function () {
$scope.bar = true;
});
});
```

After:

```javascript
angular.module('myApp', [
'btford.socket-io'
]).
factory('socket', function (socketFactory) {
return socketFactory({
prefix: 'foo~',
ioSocket: io.connect('/some/path')
});
}).
controller('MyCtrl', function (socket) {
socket.on('foo~bar', function () {
$scope.bar = true;
});
});
```

## See Also

* [ngSocket](https://github.com/jeffbcross/ngSocket)
* [angular-socket.io-mock](https://github.com/nullivex/angular-socket.io-mock)

## License
MIT
11 changes: 11 additions & 0 deletions public/components/angular-socket-io/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "angular-socket-io",
"version": "0.6.0",
"main": "socket.js",
"dependencies": {
"angular": "~1.2.6"
},
"devDependencies": {
"angular-mocks": "~1.2.6"
}
}
Loading