Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to specifying a custom port to run the WebSocket on (Closes #61) #68

Merged
merged 5 commits into from
Apr 30, 2017
Merged
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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ app.get('/', function(req, res) {
var server = http.createServer(app)

// Reload code here
// Reload attaching to server's port
reload(server, app)

// Reload using a custom port to run the websocket on
reload(8080, app);

server.listen(app.get('port'), function(){
console.log("Web server listening on port " + app.get('port'));
});
Expand Down Expand Up @@ -107,10 +111,10 @@ watch.watchTree(__dirname + "/public", function (f, curr, prev) {
### API for Express

```
reload(httpServer, expressApp, [verbose])
reload(httpServerOrPort, expressApp, [verbose])
```

- `httpServer`: The Node.js http server from the module `http`.
- `httpServerOrPort`: The Node.js http server from the module `http` **or** a port to run the reload websocket on (as a number). **Note**: It is important to specify a custom port if you have other websockets running in your application so they don't conflict.
- `expressApp`: The express app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express.
- `verbose`: If set to true, will show logging on the server and client side

Expand Down Expand Up @@ -148,8 +152,8 @@ Options:
-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
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
-v, --verbose Turns on logging on the server and client side. Defaults to false.
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
-v, --verbose Turns on logging on the server and client side. Defaults to false.
```

License
Expand Down
2 changes: 1 addition & 1 deletion lib/reload-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function refresh () {
var verboseLogging = false
var socketUrl = window.location.origin.replace(/^http(s?):\/\//, 'ws$1://')
var socketUrl = window.location.origin.replace() // This is dynamically populated by the reload.js file before it is sent to the browser
var socket

if (verboseLogging) {
Expand Down
24 changes: 20 additions & 4 deletions lib/reload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path')
var fs = require('fs')
var socketPortSpecified

var RELOAD_FILE = path.join(__dirname, './reload-client.js')

Expand All @@ -10,16 +11,29 @@ function configureApp (expressApp, verboseLogging, route) {
reloadCode = reloadCode.replace('verboseLogging = false', 'verboseLogging = true')
}

if (socketPortSpecified) {
reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/(^http(s?):\\/\\/)(.*:)(.*)/, \'ws$2://$3' + socketPortSpecified + '\')')
} else {
reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/^http(s?):\\/\\//, \'ws$1://\')')
}

expressApp.get(route || '/reload/reload.js', function (req, res) {
res.type('text/javascript')
res.send(reloadCode)
})
}

function configureServer (httpServer, verboseLogging) {
function configureServer (httpServerOrPort, verboseLogging) {
var connections = new Set()
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({ server: httpServer })
var wss

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

wss.on('connection', (ws) => {
connections.add(ws)
Expand All @@ -31,6 +45,7 @@ function configureServer (httpServer, verboseLogging) {
console.log('Reload client connected to server')
}
})

function sendMessage (message) {
if (verboseLogging) {
console.log('Sending message to ' + (connections.size) + ' connections: ' + message)
Expand All @@ -54,9 +69,10 @@ function configureServer (httpServer, verboseLogging) {
}
}

function reload (httpServer, expressApp, verboseLogging) {
function reload (httpServerOrPort, expressApp, verboseLogging) {
socketPortSpecified = typeof httpServerOrPort === 'number' ? httpServerOrPort : null
configureApp(expressApp, verboseLogging)
return configureServer(httpServer, verboseLogging)
return configureServer(httpServerOrPort, verboseLogging)
}

module.exports = reload
Expand Down