├── .gitignore ├── LICENSE ├── collaborators.md ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Max Ogden 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /collaborators.md: -------------------------------------------------------------------------------- 1 | ## Collaborators 2 | 3 | connections is only possible due to the excellent work of the following collaborators: 4 | 5 | 6 | 7 |
maxogdenGitHub/maxogden
mafintoshGitHub/mafintosh
8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var events = require('events') 2 | 3 | module.exports = function (servers) { 4 | var sockets = [] 5 | 6 | if (!Array.isArray(servers)) servers = [servers] 7 | 8 | for (var i = 0; i < servers.length; i++) { 9 | servers[i].on('connection', add) 10 | } 11 | 12 | var obj = new events.EventEmitter() 13 | obj.sockets = sockets 14 | obj.destroy = destroy 15 | obj.add = add 16 | 17 | return obj 18 | 19 | function add (socket) { 20 | sockets.push(socket) 21 | socket.on('close', onclose) 22 | obj.emit('connection', socket) 23 | } 24 | 25 | function onclose () { 26 | sockets.splice(sockets.indexOf(this), 1) 27 | obj.emit('close', this) 28 | if (sockets.length === 0) obj.emit('idle') 29 | } 30 | 31 | function destroy () { 32 | for (var i = 0; i < sockets.length; i++) { 33 | sockets[i].destroy() 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "connections", 3 | "version": "1.4.2", 4 | "description": "Keeps track of connections to an http server and provides a way to close connections", 5 | "main": "index.js", 6 | "author": "max ogden", 7 | "license": "BSD-2-Clause", 8 | "scripts": { 9 | "test": "standard" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/maxogden/connections.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/maxogden/connections/issues" 17 | }, 18 | "homepage": "https://github.com/maxogden/connections", 19 | "devDependencies": { 20 | "standard": "^6.0.7" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # connections 2 | 3 | Keeps track of connections to an http or tcp server (or any other server object with the same api) and provides a way to close connections 4 | 5 | By default, `require('http').createServer` provides no mechanism for tracking client connections and/or closing client connections 6 | 7 | [![NPM](https://nodei.co/npm/connections.png)](https://nodei.co/npm/connections/) 8 | 9 | ## usage 10 | 11 | ``` 12 | var connections = require('connections')(serverInstance) 13 | ``` 14 | 15 | You can also pass an array of server instances 16 | 17 | `connections` has `.sockets` and `.destroy` 18 | 19 | ### connections.on('idle', function() {}) 20 | 21 | called whenever all active connections have closed 22 | 23 | ### connections.on('close', function(socket) {}) 24 | 25 | called whenever a socket closes 26 | 27 | ### connections.on('connection', function (socket) {}) 28 | 29 | forwarded event from the server or servers 30 | 31 | ### connections.sockets 32 | 33 | an array of open sockets (http clients) 34 | 35 | ### connections.destroy() 36 | 37 | destroys/closes all active connections (calls .destroy() on each socket) 38 | 39 | ### connections.add(socket) 40 | 41 | manually add a socket to the connection list 42 | 43 | --------------------------------------------------------------------------------