Skip to content

Commit

Permalink
fix: add option to bind WS to a different port (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmvilas authored Jul 15, 2021
1 parent 2d81967 commit 80bb608
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/adapters/socket.io/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ class SocketIOAdapter extends Adapter {
_connect () {
return new Promise((resolve) => {
const channelNames = this.parsedAsyncAPI.channelNames()
const url = new URL(this.AsyncAPIServer.url())
const serverUrl = new URL(this.serverUrlExpanded)
const asyncapiServerPort = serverUrl.port || 80
const optionsPort = this.glee.options?.websocket?.port
const port = optionsPort || asyncapiServerPort

const serverOptions = {
path: url.pathname || '/',
path: serverUrl.pathname || '/',
serveClient: false,
transports: ['websocket'],
}

if (this.glee.options.websocket.httpServer) {
const server = this.glee.options.websocket.httpServer
if (String(server.address().port) !== String(url.port)) {
console.error(`Your custom HTTP server is listening on port ${server.address().port} but your AsyncAPI file says it must listen on ${url.port}. Please fix the inconsistency.`)
if (!optionsPort && String(server.address().port) !== String(port)) {
console.error(`Your custom HTTP server is listening on port ${server.address().port} but your AsyncAPI file says it must listen on ${port}. Please fix the inconsistency.`)
process.exit(1)
}
this.server = new Server(server, serverOptions)
Expand All @@ -55,7 +58,7 @@ class SocketIOAdapter extends Adapter {
})

if (!this.glee.options.websocket.httpServer) {
this.server.listen(url.port || 80)
this.server.listen(port)
}
resolve(this)
})
Expand Down
10 changes: 6 additions & 4 deletions src/adapters/ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class WebSocketsAdapter extends Adapter {
const serverUrl = new URL(this.serverUrlExpanded)
const wsHttpServer = this.glee.options?.websocket?.httpServer || http.createServer()
const asyncapiServerPort = serverUrl.port || 80
const optionsPort = this.glee.options?.websocket?.port
const port = optionsPort || asyncapiServerPort

if (this.glee.options?.websocket?.httpServer && String(wsHttpServer.address().port) !== String(asyncapiServerPort)) {
console.error(`Your custom HTTP server is listening on port ${wsHttpServer.address().port} but your AsyncAPI file says it must listen on ${asyncapiServerPort}. Please fix the inconsistency.`)
if (!optionsPort && this.glee.options?.websocket?.httpServer && String(wsHttpServer.address().port) !== String(port)) {
console.error(`Your custom HTTP server is listening on port ${wsHttpServer.address().port} but your AsyncAPI file says it must listen on ${port}. Please fix the inconsistency.`)
process.exit(1)
}

Expand Down Expand Up @@ -103,15 +105,15 @@ class WebSocketsAdapter extends Adapter {
this.emit('message', msg, ws)
})

this.emit('connection', { name: this.name(), adapter: this, connection: ws, channel: pathname })
this.emit('connect', { name: this.name(), adapter: this, connection: ws, channel: pathname })
})
} else {
socket.destroy()
}
})

if (!this.glee.options?.websocket?.httpServer) {
wsHttpServer.listen(asyncapiServerPort)
wsHttpServer.listen(port)
}

this.emit('server:ready', { name: this.name(), adapter: this })
Expand Down
2 changes: 1 addition & 1 deletion src/lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GleeAdapter extends EventEmitter {
this.serverUrlExpanded = uriTemplates(this.AsyncAPIServer.url()).fill(uriTemplateValues)

this.on('error', err => { this.glee.injectError(err) })
this.on('message', (message, connection) => {
this.on('message', (message, connection) => {
const conn = new GleeConnection({
connection,
channels: this.connections.find(c => c.rawConnection === connection).channels,
Expand Down

0 comments on commit 80bb608

Please sign in to comment.