Skip to content

Commit 3569408

Browse files
c-wdelucis
andauthored
Add option to run server over HTTPS (#631)
Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 4759305 commit 3569408

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

docs/documentation/api/Server.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ server.run(8000);
6262
server.run(8000, () => console.log("server running..."));
6363
```
6464

65+
##### With HTTPS
66+
67+
```js
68+
const { Server } = require('boardgame.io/server');
69+
const fs = require('fs');
70+
71+
const server = Server({
72+
games: [game1, game2, ...],
73+
74+
https: {
75+
cert: fs.readFileSync('/path/to/cert'),
76+
key: fs.readFileSync('/path/to/key'),
77+
},
78+
});
79+
80+
server.run(8000);
81+
```
82+
6583
##### With custom authentication
6684

6785
`generateCredentials` is called when a player joins a game with:

src/server/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ interface ServerConfig {
2626
};
2727
}
2828

29+
interface HttpsOptions {
30+
cert: string;
31+
key: string;
32+
}
33+
2934
/**
3035
* Build config object from server run arguments.
3136
*/
@@ -59,6 +64,7 @@ interface ServerOpts {
5964
transport?;
6065
authenticateCredentials?: ServerTypes.AuthenticateCredentials;
6166
generateCredentials?: ServerTypes.GenerateCredentials;
67+
https?: HttpsOptions;
6268
}
6369

6470
/**
@@ -69,13 +75,15 @@ interface ServerOpts {
6975
* @param transport - The interface with the clients.
7076
* @param authenticateCredentials - Function to test player credentials.
7177
* @param generateCredentials - Method for API to generate player credentials.
78+
* @param https - HTTPS configuration options passed through to the TLS module.
7279
*/
7380
export function Server({
7481
games,
7582
db,
7683
transport,
7784
authenticateCredentials,
7885
generateCredentials,
86+
https,
7987
}: ServerOpts) {
8088
const app = new Koa();
8189

@@ -91,7 +99,10 @@ export function Server({
9199
typeof authenticateCredentials === 'function'
92100
? authenticateCredentials
93101
: true;
94-
transport = SocketIO({ auth });
102+
transport = SocketIO({
103+
auth,
104+
https,
105+
});
95106
}
96107
transport.init(app, games);
97108

src/server/transport/socketio.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export function SocketIO({
6262
clientInfo = new Map(),
6363
roomInfo = new Map(),
6464
auth = true,
65+
https,
6566
} = {}) {
6667
return {
6768
init: (app, games) => {
@@ -73,7 +74,7 @@ export function SocketIO({
7374
});
7475

7576
app.context.io = io;
76-
io.attach(app);
77+
io.attach(app, !!https, https);
7778

7879
for (const game of games) {
7980
const nsp = app._io.of(game.name);

0 commit comments

Comments
 (0)