Skip to content

Commit

Permalink
Merge 54c20d4 into 9399f53
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserf committed Sep 17, 2019
2 parents 9399f53 + 54c20d4 commit a46a9fb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## [4.2.2] - 2019.09.17

### Fix

Adding health-checks for all ws based endpoints.

## [4.2.1] - 2019.09.17

### Fix
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@deepstream/server",
"version": "4.2.1",
"version": "4.2.2",
"description": "a scalable server for realtime webapps",
"main": "./dist/src/deepstream.io.js",
"bin": {
Expand Down
25 changes: 16 additions & 9 deletions src/connection-endpoint/json/connection-endpoint.ts
Expand Up @@ -3,7 +3,7 @@ import {createWSSocketWrapper} from './socket-wrapper-factory'
import { DeepstreamServices, SocketWrapper, DeepstreamConfig, UnauthenticatedSocketWrapper } from '../../../ds-types/src/index'
import { Dictionary } from 'ts-essentials'
import * as WebSocket from 'ws'
import { IncomingMessage } from 'http'
import { IncomingMessage, Server } from 'http'

/**
* This is the frontmost class of deepstream's message pipeline. It receives
Expand All @@ -13,27 +13,34 @@ import { IncomingMessage } from 'http'
export class WSJSONConnectionEndpoint extends ConnectionEndpoint {
private server!: WebSocket.Server
private connections = new Map<WebSocket, UnauthenticatedSocketWrapper>()
private httpServer: Server

constructor (private wsOptions: WebSocketServerConfig, services: DeepstreamServices, config: DeepstreamConfig) {
super(wsOptions, services, config)
this.description = 'WS JSON Connection Endpoint (ONLY FOR SDK DEVELOPMENT)'
this.onMessages = this.onMessages.bind(this)
this.httpServer = this.wsOptions.httpServer ? this.wsOptions.httpServer : new Server()
}

/**
* Initialize the ws endpoint, setup callbacks etc.
*/
public createWebsocketServer () {
this.server = new WebSocket.Server({
server: this.httpServer
})
if (this.wsOptions.httpServer) {
this.server = new WebSocket.Server({
server: this.wsOptions.httpServer
})
process.nextTick(this.onReady.bind(this))
} else {
this.server = new WebSocket.Server({
port: this.getOption('port'),
host: this.getOption('host')
}, () => this.onReady())
this.httpServer.on('request', (request, response) => {
if (request.url === this.wsOptions.healthCheckPath && request.method === 'GET') {
response.end()
} else {
response.writeHead(404)
response.end(`Only ${this.wsOptions.healthCheckPath} supported`)
}
})
this.httpServer.listen(this.getOption('port'), this.getOption('host'), this.onReady.bind(this))
}

this.server.on('connection', (websocket, request) => {
Expand Down Expand Up @@ -73,7 +80,7 @@ export class WSJSONConnectionEndpoint extends ConnectionEndpoint {
})
await Promise.all(closePromises)
this.connections.clear()
return new Promise((resolve) => this.server.close(resolve))
return new Promise((resolve) => this.httpServer.close(resolve))
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/connection-endpoint/text/connection-endpoint.ts
Expand Up @@ -22,6 +22,7 @@ export class WSTextConnectionEndpoint extends ConnectionEndpoint {
private server!: WebSocket.Server
private connections = new Map<WebSocket, UnauthenticatedSocketWrapper>()
private pingMessage: string
private httpServer: Server

constructor (private wsOptions: WSConnectionEndpointConfig, services: DeepstreamServices, config: DeepstreamConfig) {
super(wsOptions, services, config)
Expand All @@ -31,22 +32,28 @@ export class WSTextConnectionEndpoint extends ConnectionEndpoint {
topic: TOPIC.CONNECTION,
action: CONNECTION_ACTION.PING
})
this.httpServer = this.wsOptions.httpServer ? this.wsOptions.httpServer : new Server()
}

/**
* Initialize the ws endpoint, setup callbacks etc.
*/
public createWebsocketServer () {
this.server = new WebSocket.Server({
server: this.httpServer
})
if (this.wsOptions.httpServer) {
this.server = new WebSocket.Server({
server: this.wsOptions.httpServer
})
process.nextTick(this.onReady.bind(this))
} else {
this.server = new WebSocket.Server({
port: this.getOption('port'),
host: this.getOption('host')
}, () => this.onReady())
this.httpServer.on('request', (request, response) => {
if (request.url === this.wsOptions.healthCheckPath && request.method === 'GET') {
response.end()
} else {
response.writeHead(404)
response.end(`Only ${this.wsOptions.healthCheckPath} supported`)
}
})
this.httpServer.listen(this.getOption('port'), this.getOption('host'), this.onReady.bind(this))
}

this.server.on('connection', (websocket, request) => {
Expand Down Expand Up @@ -104,7 +111,7 @@ export class WSTextConnectionEndpoint extends ConnectionEndpoint {
})
await Promise.all(closePromises)
this.connections.clear()
return new Promise((resolve) => this.server.close(resolve))
return new Promise((resolve) => this.httpServer.close(resolve))
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/connection-endpoint/websocket/connection-endpoint.ts
Expand Up @@ -9,6 +9,7 @@ export interface WebSocketServerConfig {
outgoingBufferTimeout: number,
maxBufferByteSize: number,
headers: string[],
healthCheckPath: string,
[index: string]: any,
}

Expand Down
23 changes: 15 additions & 8 deletions src/connection-endpoint/ws/connection-endpoint.ts
Expand Up @@ -18,27 +18,34 @@ interface WSConnectionEndpointConfig extends WebSocketServerConfig {
export class WSConnectionEndpoint extends ConnectionEndpoint {
private server!: WebSocket.Server
private connections = new Map<WebSocket, UnauthenticatedSocketWrapper>()
private httpServer!: Server

constructor (private wsOptions: WSConnectionEndpointConfig, services: DeepstreamServices, config: DeepstreamConfig) {
super(wsOptions, services, config)
this.description = 'WS Connection Endpoint'
this.onMessages = this.onMessages.bind(this)
this.httpServer = this.wsOptions.httpServer ? this.wsOptions.httpServer : new Server()
}

/**
* Initialize the ws endpoint, setup callbacks etc.
*/
public createWebsocketServer () {
this.server = new WebSocket.Server({
server: this.httpServer
})
if (this.wsOptions.httpServer) {
this.server = new WebSocket.Server({
server: this.wsOptions.httpServer
})
process.nextTick(this.onReady.bind(this))
} else {
this.server = new WebSocket.Server({
port: this.getOption('port'),
host: this.getOption('host')
}, () => this.onReady())
this.httpServer.on('request', (request, response) => {
if (request.url === this.wsOptions.healthCheckPath && request.method === 'GET') {
response.end()
} else {
response.writeHead(404)
response.end(`Only ${this.wsOptions.healthCheckPath} supported`)
}
})
this.httpServer.listen(this.getOption('port'), this.getOption('host'), this.onReady.bind(this))
}

this.server.on('connection', (websocket, request) => {
Expand Down Expand Up @@ -80,7 +87,7 @@ export class WSConnectionEndpoint extends ConnectionEndpoint {
})
await Promise.all(closePromises)
this.connections.clear()
return new Promise((resolve) => this.server.close(resolve))
return new Promise((resolve) => this.httpServer.close(resolve))
}

/**
Expand Down

0 comments on commit a46a9fb

Please sign in to comment.