├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 'Dominic Tarr' 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # on-change-network 2 | 3 | Call a listener whenever the network interface changes. 4 | I.e. detect when the local user has changed to another wifi network. 5 | 6 | ## example 7 | 8 | ``` js 9 | require('on-change-network')(function () { 10 | console.log('wifi changed') 11 | }) 12 | ``` 13 | 14 | ## License 15 | 16 | MIT 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var os = require('os') 2 | 3 | function each (network) { 4 | return Object.keys(network).map(function (interface) { 5 | return network[interface].filter(function (e) { 6 | return !e.internal 7 | }).map(function (e) { 8 | return interface+'/'+e.address 9 | }) 10 | }).reduce(function (a, b) { 11 | return a.concat(b) 12 | }, []) 13 | } 14 | 15 | module.exports = function (onNetwork, ref) { 16 | var init = each(os.networkInterfaces()).join(',') 17 | var int = setInterval(function (e) { 18 | var cur = each(os.networkInterfaces()).join(',') 19 | if(init !== cur) 20 | onNetwork(init = cur) 21 | }, 1000) 22 | 23 | //unreference the counter (only on node) 24 | if(!ref && int.unref) int.unref() 25 | } 26 | 27 | if(!module.parent) 28 | module.exports(function (addrs) { 29 | console.log(addrs) 30 | }, true) 31 | 32 | 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "on-change-network", 3 | "description": "detect when computer changes (wifi) networks", 4 | "version": "0.0.2", 5 | "homepage": "https://github.com/dominictarr/on-change-network", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dominictarr/on-change-network.git" 9 | }, 10 | "author": "'Dominic Tarr' (dominictarr.com)", 11 | "license": "MIT" 12 | } 13 | --------------------------------------------------------------------------------