Skip to content

Commit

Permalink
Merge pull request #26 from alallier/fixingFlags
Browse files Browse the repository at this point in the history
Fixed flags, added a new flag, and updated documentation.
  • Loading branch information
jprichardson committed Sep 28, 2015
2 parents beb797c + 4a47f36 commit 937cba2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.4.0 / 2015-08-17
------------------
- add `true` option to `delay` so that it weights indefinitely until server is up https://github.com/jprichardson/reload/pull/21
- add `true` option to `delay` so that it waits indefinitely until server is up https://github.com/jprichardson/reload/pull/21
- express 4 routes, https://github.com/jprichardson/reload/pull/24

0.3.0 / 2015-07-17
Expand Down
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ app.get('/', function(req, res) {
var server = http.createServer(app)

//reload code here
//optional server delay argument can be given to reload, refer to API below
reload(server, app)
//optional reload delay and wait argument can be given to reload, refer to [API](https://github.com/jprichardson/reload#api) below
reload(server, app, [reloadDelay], [wait])

server.listen(app.get('port'), function(){
console.log("Web server listening on port " + app.get('port'));
Expand Down Expand Up @@ -99,8 +99,8 @@ app.get('/', function(req, res) {
var server = http.createServer(app)

//reload code here
//optional server delay argument can be given to reload, refer to API below
reload(server, app)
//optional reload delay and wait argument can be given to reload, refer to [API](https://github.com/jprichardson/reload#api) below
reload(server, app, [reloadDelay], [wait])

server.listen(app.get('port'), function(){
console.log("Web server listening on port " + app.get('port'));
Expand Down Expand Up @@ -148,14 +148,15 @@ Usage: reload [options]
Options:
-h, --help Output usage information
-V, --version Output the version number
-b, --browser Open in the browser automatically.
-d, --dir [dir] The directory to serve up. Defaults to current dir.
-e, --exts [extensions] Extensions separated by commas or pipes. Defaults to html,js,css.
-p, --port [port] The port to bind to. Can be set with PORT env variable as well. Defaults to 8080
-d, --delay [delay] How long (ms) should the browser wait before reconnecting? You can also specify true, if you would like reload to wait until the server comes back up before reloading the page. Defaults to 300 ms.
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
-h, --help Output usage information
-V, --version Output the version number
-b, --browser Open in the browser automatically.
-d, --dir [dir] The directory to serve up. Defaults to current dir.
-e, --exts [extensions] Extensions separated by commas or pipes. Defaults to html,js,css.
-p, --port [port] The port to bind to. Can be set with PORT env variable as well. Defaults to 8080
-r, --reload-delay [reload-delay] The client side refresh time in milliseconds. Default is `300`. If wait is specified as true this delay becomes the delay of how long the pages waits to reload after the socket is reopened.
-w, --wait [wait] Specify true, if you would like reload to wait until the server comes back up before reloading the page.
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
```
Expand All @@ -171,18 +172,19 @@ this will open your `index.html` file in the browser. Any changes that you make
How does it work?
-----------------

It's actually stupidly simple. We leverage `supervisor` to restart the server if any file changes. The client side keeps a websocket open, once the websocket closes, the client sets a timeout to reload in approximately 300 ms. Simple huh?
It's actually stupidly simple. We leverage `supervisor` to restart the server if any file changes. The client side keeps a websocket open, once the websocket closes, the client sets a timeout to reload in approximately 300 ms (or any other time you'd like, refer to [API](https://github.com/jprichardson/reload#api) below. Simple huh?



API
---

### reload(httpServer, expressApp, [delay])
### reload(httpServer, expressApp, [reloadDelay], [wait])

- `httpServer`: The Node.js http server from the module `http`.
- `expressApp`: The express app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express.
- `delay`: The client side refresh time in milliseconds. Default is `300`. You can also specify true, if you would like reload to wait until the server comes back up before reloading the page.
- `reloadDelay`: The client side refresh time in milliseconds. Default is `300`. If --wait is true, then this delay is used to determine how long to wait before attempting to reopen after web socket opens.
- `wait`: If wait is specified as true reload will wait until the server comes back up before reloading the page.



Expand All @@ -193,4 +195,3 @@ License

Copyright 2013, JP Richardson <[email protected]>


5 changes: 3 additions & 2 deletions bin/reload
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ program.version(require('../package.json').version)
.option('-d, --dir [dir]', 'The directory to serve up. Defaults to current dir.', process.cwd())
.option('-e, --exts [extensions]', 'Extensions separated by commas or pipes. Defaults to html,js,css.', 'html|js|css')
.option('-p, --port [port]', 'The port to bind to. Can be set with PORT env variable as well. Defaults to 8080', '8080')
.option('-d, --delay [delay]', 'How long (ms) should the browser wait before reconnecting? Defaults to 300 ms. You can also specify true, if you would like reload to wait until the server comes back up before reloading the page.', '300')
.option('-r, --reload-delay [reload-delay]', 'How long (ms) should the server wait before refreshing the page? Defaults to 300 ms. If -w is set to true, this delay becomes how long the page should wait to reload after the socket open.', '300')
.option('-w, --wait [wait]', 'Specify whether or not reload should wait until the server comes back up before reloading the page. Defaults to true', 'true')
.option('-s, --start-page [start-page]', 'Specify a start page. Defaults to index.html', 'index.html')
.parse(process.argv)

Expand All @@ -23,7 +24,7 @@ var serverFile = path.join(__dirname, '../lib/reload-server.js')
if (program.exts.indexOf(','))
program.exts = program.exts.replace(/\,/g,'|') //replace comma for pipe, that's what supervisor likes

var args = ['-e', 'html|js|css', '-q', '--', serverFile, program.port, program.dir, !!program.browser, runFile, program.time, program.startPage]
var args = ['-e', 'html|js|css', '-q', '--', serverFile, program.port, program.dir, !!program.browser, runFile, program.reloadDelay, program.wait, program.startPage]
supervisor.run(args)

console.log("\n Reload web server:")
Expand Down
14 changes: 9 additions & 5 deletions lib/reload-client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
;(function refresh () {
var RLD_TIMEOUT = 300;
var sock = new SockJS(window.location.origin + '/sockreload');
var waitForServer = false;
var reloadDelay = 300;
var socketDelay = 0;
var wait = false;
var sock;
var checkDelay = 1;
var checkDelayCounter = 1;
Expand All @@ -12,7 +14,9 @@

//if the server is up then, the sockert will reach this event handler and then call refreshPage
sock.onopen = function() {
window.location.reload();
setTimeout(function() {
window.location.reload()
}, socketDelay);
};

//else the server is down try and connect again with another call to newConn
Expand All @@ -29,7 +33,7 @@

sock.onclose = function() {
//check for the server only if the user entered in true for a delay
if (waitForServer) {
if (wait) {
sock = null;

newConn();
Expand All @@ -38,7 +42,7 @@
else {
setTimeout(function() {
window.location.reload();
},RLD_TIMEOUT);
},reloadDelay);
}
};
})();
})();
9 changes: 5 additions & 4 deletions lib/reload-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ var port = process.argv[2]
, dir = process.argv[3]
, openBrowser = (process.argv[4] === 'true')
, runFile = process.argv[5]
, delay = process.argv[6]
, startPage = process.argv[7]
, reloadDelay = process.argv[6]
, wait = process.argv[7]
, startPage = process.argv[8]

var router = express.Router();

Expand All @@ -24,7 +25,7 @@ router.get('/', function(req, res, next) {
if (itDoes)
sendhtml(startFile, res)
else
res.send(404, "Can't find " + startPage)
res.status(404).send("Can't fine " + startPage);
})
})

Expand All @@ -40,7 +41,7 @@ app.use('*html', router);
app.use(express.static(dir)) //should cache static assets

var server = http.createServer(app)
reload(server, app, delay)
reload(server, app, reloadDelay, wait)

server.listen(app.get('port'), function(){
if (!fs.existsSync(runFile)) {
Expand Down
14 changes: 7 additions & 7 deletions lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ var sockjs = require('sockjs')
var SOCKJS_FILE = path.join(__dirname, './sockjs-0.3-min.js')
, RELOAD_FILE = path.join(__dirname, './reload-client.js')

module.exports = function reload (httpServer, expressApp, delay) {
module.exports = function reload (httpServer, expressApp, reloadDelay, wait) {
//this happens at startup of program, so sync is alright
var sockjsCode = fs.readFileSync(SOCKJS_FILE, 'utf8')
, reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8')

// If delay is specified as true then reload will wait for the server to be up before reloading the page
if (delay === true) {
reloadCode = reloadCode.replace('waitForServer = false;', 'waitForServer = true;')
}
else if (delay > 0) {
reloadCode = reloadCode.replace('RLD_TIMEOUT = 300', 'RLD_TIMEOUT = ' + delay)
//if reloadDelay is specified as true then reload will wait for the server to be up before reloading the page
if (wait === 'true') {
reloadCode = reloadCode.replace('wait = false;', 'wait = true;')
reloadCode = reloadCode.replace('socketDelay = 0;', 'socketDelay = ' + reloadDelay)
}

reloadCode = reloadCode.replace('reloadDelay = 300;', 'reloadDelay = ' + reloadDelay)

var clientCode = sockjsCode + '\n\n' + reloadCode

Expand Down

0 comments on commit 937cba2

Please sign in to comment.