Skip to content

Commit 557b66c

Browse files
committed
add run() to Server
1 parent 2a85b40 commit 557b66c

6 files changed

Lines changed: 39 additions & 31 deletions

File tree

docs/api/Server.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ The `games` argument takes a list of game implementations
1717

1818
### Returns
1919

20-
(`app`): A Koa app.
20+
An object that contains:
21+
22+
1. run (_function_): A function to run the server.
23+
Signature: (port, callback) => {}
24+
2. app (_object_): The Koa app.
2125

2226
### Usage
2327

2428
```js
2529
const Server = require('boardgame.io/server');
2630

27-
const app = Server({
31+
const server = Server({
2832
games: [game1, game2, ...],
2933

3034
// Optional, if you want to hook it up to a
@@ -35,5 +39,5 @@ const app = Server({
3539
db: new DbImpl(),
3640
});
3741

38-
app.listen(8000);
42+
server.run(8000);
3943
```

docs/multiplayer.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ are playing. Here is a snippet showing how to set it up
3030
to serve the socket requests coming from the client.
3131

3232
```js
33-
const Server = require('boardgame.io/server');
33+
const Server = require('boardgame.io/server').Server;
3434
const TicTacToe = require('./tic-tac-toe');
35-
const app = Server({ games: [TicTacToe] });
36-
app.listen(8000);
35+
const server = Server({ games: [TicTacToe] });
36+
server.run(8000);
3737
```
3838

3939
You can also serve multiple types of games from the same server:
@@ -69,17 +69,17 @@ URL be synced to the same game.
6969

7070
In a real app, you might want to also serve your React
7171
frontend from the same server as well. The returned object
72-
`app` is a [Koa](http://koajs.com/) app that you can
73-
use to attach other handlers etc.
72+
contains `app`, which is a [Koa](http://koajs.com/) app that
73+
you can use to attach other handlers etc.
7474

7575
```js
7676
const KoaStatic = require('koa-static');
7777
const Server = require('boardgame.io/server');
7878
const TicTacToe = require('./tic-tac-toe');
7979

80-
const app = Server({ games: [TicTacToe] });
81-
app.use(KoaStatic('path/to/dir'));
82-
app.listen(8000);
80+
const server = Server({ games: [TicTacToe] });
81+
server.app.use(KoaStatic('path/to/dir'));
82+
server.run(8000);
8383
```
8484

8585
You might also want to keep them separate (one server to serve the web app

examples/server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ import KoaStatic from 'koa-static';
1111
import KoaHelmet from 'koa-helmet';
1212
import KoaWebpack from 'koa-webpack';
1313
import WebpackConfig from './webpack.dev.js';
14-
import Server from 'boardgame.io/server';
14+
import { Server } from 'boardgame.io/server';
1515
import TicTacToe from './modules/tic-tac-toe/game';
1616
import Chess from './modules/chess/game';
1717

1818
const PORT = process.env.PORT || 8000;
1919
const DEV = process.env.NODE_ENV === 'development';
2020
const PROD = !DEV;
2121

22-
const app = Server({ games: [TicTacToe, Chess] });
22+
const server = Server({ games: [TicTacToe, Chess] });
2323

2424
if (DEV) {
25-
app.use(
25+
server.app.use(
2626
KoaWebpack({
2727
config: WebpackConfig,
2828
})
2929
);
3030
}
3131

3232
if (PROD) {
33-
app.use(KoaStatic(path.join(__dirname, 'dist')));
34-
app.use(KoaHelmet());
33+
server.app.use(KoaStatic(path.join(__dirname, 'dist')));
34+
server.app.use(KoaHelmet());
3535
}
3636

37-
app.listen(PORT, () => {
37+
server.run(PORT, () => {
3838
console.log(`Serving at: http://localhost:${PORT}/`);
3939
});

packages/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* https://opensource.org/licenses/MIT.
77
*/
88

9-
import Server from '../src/server/index.js';
9+
import { Server } from '../src/server/index.js';
1010

11-
export default Server;
11+
export { Server };

src/server/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createGameReducer } from '../core/reducer';
1414
const PING_TIMEOUT = 20 * 1e3;
1515
const PING_INTERVAL = 10 * 1e3;
1616

17-
function Server({ games, db, _clientInfo, _roomInfo }) {
17+
export function Server({ games, db, _clientInfo, _roomInfo }) {
1818
const app = new Koa();
1919
const io = new IO({
2020
ioOptions: {
@@ -28,7 +28,6 @@ function Server({ games, db, _clientInfo, _roomInfo }) {
2828
if (db === undefined) {
2929
db = new InMemory();
3030
}
31-
db.connect();
3231

3332
const clientInfo = _clientInfo || new Map();
3433
const roomInfo = _roomInfo || new Map();
@@ -130,7 +129,11 @@ function Server({ games, db, _clientInfo, _roomInfo }) {
130129
});
131130
}
132131

133-
return app;
132+
return {
133+
app,
134+
run: async (port, callback) => {
135+
await db.connect();
136+
app.listen(port, callback);
137+
},
138+
};
134139
}
135-
136-
export default Server;

src/server/index.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* https://opensource.org/licenses/MIT.
77
*/
88

9-
import Server from './index';
9+
import { Server } from './index';
1010
import Game from '../core/game';
1111
import * as ActionCreators from '../core/action-creators';
1212
import * as Redux from 'redux';
@@ -61,6 +61,7 @@ const game = Game({ seed: 0 });
6161

6262
test('basic', () => {
6363
const server = Server({ games: [game] });
64+
server.run();
6465
expect(server).not.toBe(undefined);
6566
});
6667

@@ -77,7 +78,7 @@ test('connect / disconnect', async () => {
7778
const _roomInfo = new Map();
7879

7980
const server = Server({ games: [game], _clientInfo, _roomInfo });
80-
const io = server.context.io;
81+
const io = server.app.context.io;
8182

8283
io.socket.id = '0';
8384
await io.socket.receive('sync', 'gameID', '0', 2);
@@ -115,7 +116,7 @@ test('connect / disconnect', async () => {
115116

116117
test('sync', async () => {
117118
const server = Server({ games: [game] });
118-
const io = server.context.io;
119+
const io = server.app.context.io;
119120
expect(server).not.toBe(undefined);
120121

121122
const spy = jest.spyOn(Redux, 'createStore');
@@ -139,7 +140,7 @@ test('sync', async () => {
139140

140141
test('action', async () => {
141142
const server = Server({ games: [game] });
142-
const io = server.context.io;
143+
const io = server.app.context.io;
143144
const action = ActionCreators.gameEvent('endTurn');
144145

145146
await io.socket.receive('action', action);
@@ -218,7 +219,7 @@ test('playerView (sync)', async () => {
218219
});
219220

220221
const server = Server({ games: [game] });
221-
const io = server.context.io;
222+
const io = server.app.context.io;
222223

223224
await io.socket.receive('sync', 'gameID', 0);
224225
expect(io.socket.emit).toHaveBeenCalledTimes(1);
@@ -232,7 +233,7 @@ test('playerView (action)', async () => {
232233
},
233234
});
234235
const server = Server({ games: [game] });
235-
const io = server.context.io;
236+
const io = server.app.context.io;
236237
const action = ActionCreators.gameEvent('endTurn');
237238

238239
io.socket.id = 'first';
@@ -272,7 +273,7 @@ test('custom db implementation', async () => {
272273

273274
const game = Game({});
274275
const server = Server({ games: [game], db: new Custom() });
275-
const io = server.context.io;
276+
const io = server.app.context.io;
276277

277278
await io.socket.receive('sync', 'gameID');
278279
expect(getId).toBe('gameID');

0 commit comments

Comments
 (0)