Skip to content

Commit

Permalink
Merge pull request #130 from alallier/serverWait
Browse files Browse the repository at this point in the history
Added socket server wait to start option
  • Loading branch information
alallier authored Jul 25, 2017
2 parents 483e6cb + c3abee5 commit 202e013
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ reload(app, opts)

##### Table of options for reload opts parameter

| Parameter Name | Type | Description | Optional | Default |
|----------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|
| port | number | Port to run reload on. || `9856` |
| route | string | Route that reload should use to serve the client side script file. Changing the route will require the script tag URL to change. Reload will always strip any occurrence of reload.js and append reload.js for you. This is to ensure case, order, and use of / is correct. For example specifying newRoutePath as the route will give reload a route of newRoutePath/reload.js. (Recommend not modifying). || `reload` |
| verbose | boolean | If set to true, will show logging on the server and client side. || `false` |
| Parameter Name | Type | Description | Optional | Default |
|--------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|----------|
| port | number | Port to run reload on. || `9856` |
| webSocketServerWaitStart | boolean | When enabled will delay starting and opening WebSocket server when requiring reload. After enabling use the `startWebSocketServer` function returned in the object provided by the API to start the WebSocket. **_Note_**: Failing to call the returned function with this option enabled will cause reload not to work. See [return API](Returns) for more information || `false` |
| route | string | Route that reload should use to serve the client side script file. Changing the route will require the script tag URL to change. Reload will always strip any occurrence of reload.js and append reload.js for you. This is to ensure case, order, and use of / is correct. For example specifying newRoutePath as the route will give reload a route of newRoutePath/reload.js. (Recommend not modifying). || `reload` |
| verbose | boolean | If set to true, will show logging on the server and client side. || `false` |

##### Upgrading to version 2

Expand All @@ -167,9 +168,10 @@ To read more about the API breaking changes please refer to the [changelog](CHAN

An **object** containing:

| Name | Type | Description |
|--------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| reload | function | A function that when called reloads all connected clients. For more information see [manually firing server-side reload events](#manually-firing-server-side-reload-events).|
| Name | Type | Description |
|----------------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| reload | function | A function that when called reloads all connected clients. For more information see [manually firing server-side reload events](#manually-firing-server-side-reload-events). |
| startWebSocketServer | function | Starts and opens the WebSocket server required for reload. Only active when using the optional parameter `webSocketServerWaitStart`. Read the [parameters](#table-of-options-for-reload-opts-parameter) for more information |

Using reload as a command line application
---
Expand Down
36 changes: 26 additions & 10 deletions lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function reload (app, opts, server) {
var expressApp
var verboseLogging
var port
var webSocketServerWaitStart

// Application variables
var RELOAD_FILE = path.join(__dirname, './reload-client.js')
Expand Down Expand Up @@ -39,8 +40,9 @@ module.exports = function reload (app, opts, server) {
} else { // Setup options or use defaults
expressApp = argumentZero
port = opts.port || 9856

webSocketServerWaitStart = opts.webSocketServerWaitStart
route = opts.route

if (route) {
// If reload.js is found in the route option strip it. We will concat it for user to ensure no case errors or order problems.
reloadJsMatch = route.match(/reload\.js/i)
Expand Down Expand Up @@ -90,19 +92,28 @@ module.exports = function reload (app, opts, server) {
res.send(reloadCode)
})

// Websocket server setup
// Use custom user specified port
if (socketPortSpecified) {
wss = new WebSocketServer({ port: httpServerOrPort })
} else { // Attach to server, using server's port. Kept here to support legacy arguments.
wss = new WebSocketServer({ server: httpServerOrPort })
if (!webSocketServerWaitStart) {
startWebSocketServer()
}

wss.on('connection', (ws) => {
// Websocket server setup
function startWebSocketServer () {
if (verboseLogging) {
console.log('Reload client connected to server')
console.log('Starting WebSocket Server')
}
})

if (socketPortSpecified) { // Use custom user specified port
wss = new WebSocketServer({ port: httpServerOrPort })
} else { // Attach to server, using server's port. Kept here to support legacy arguments.
wss = new WebSocketServer({ server: httpServerOrPort })
}

wss.on('connection', (ws) => {
if (verboseLogging) {
console.log('Reload client connected to server')
}
})
}

function sendMessage (message) {
if (verboseLogging) {
Expand All @@ -119,6 +130,11 @@ module.exports = function reload (app, opts, server) {
return {
'reload': function () {
sendMessage('reload')
},
'startWebSocketServer': function () {
if (webSocketServerWaitStart) {
startWebSocketServer()
}
}
}
}

0 comments on commit 202e013

Please sign in to comment.