Skip to content

Commit

Permalink
add run() to Server
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Mar 1, 2018
1 parent 2a85b40 commit 557b66c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
10 changes: 7 additions & 3 deletions docs/api/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ The `games` argument takes a list of game implementations

### Returns

(`app`): A Koa app.
An object that contains:

1. run (_function_): A function to run the server.
Signature: (port, callback) => {}
2. app (_object_): The Koa app.

### Usage

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

const app = Server({
const server = Server({
games: [game1, game2, ...],

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

app.listen(8000);
server.run(8000);
```
16 changes: 8 additions & 8 deletions docs/multiplayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ are playing. Here is a snippet showing how to set it up
to serve the socket requests coming from the client.

```js
const Server = require('boardgame.io/server');
const Server = require('boardgame.io/server').Server;
const TicTacToe = require('./tic-tac-toe');
const app = Server({ games: [TicTacToe] });
app.listen(8000);
const server = Server({ games: [TicTacToe] });
server.run(8000);
```

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

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

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

const app = Server({ games: [TicTacToe] });
app.use(KoaStatic('path/to/dir'));
app.listen(8000);
const server = Server({ games: [TicTacToe] });
server.app.use(KoaStatic('path/to/dir'));
server.run(8000);
```

You might also want to keep them separate (one server to serve the web app
Expand Down
12 changes: 6 additions & 6 deletions examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import KoaStatic from 'koa-static';
import KoaHelmet from 'koa-helmet';
import KoaWebpack from 'koa-webpack';
import WebpackConfig from './webpack.dev.js';
import Server from 'boardgame.io/server';
import { Server } from 'boardgame.io/server';
import TicTacToe from './modules/tic-tac-toe/game';
import Chess from './modules/chess/game';

const PORT = process.env.PORT || 8000;
const DEV = process.env.NODE_ENV === 'development';
const PROD = !DEV;

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

if (DEV) {
app.use(
server.app.use(
KoaWebpack({
config: WebpackConfig,
})
);
}

if (PROD) {
app.use(KoaStatic(path.join(__dirname, 'dist')));
app.use(KoaHelmet());
server.app.use(KoaStatic(path.join(__dirname, 'dist')));
server.app.use(KoaHelmet());
}

app.listen(PORT, () => {
server.run(PORT, () => {
console.log(`Serving at: http://localhost:${PORT}/`);
});
4 changes: 2 additions & 2 deletions packages/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* https://opensource.org/licenses/MIT.
*/

import Server from '../src/server/index.js';
import { Server } from '../src/server/index.js';

export default Server;
export { Server };
13 changes: 8 additions & 5 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createGameReducer } from '../core/reducer';
const PING_TIMEOUT = 20 * 1e3;
const PING_INTERVAL = 10 * 1e3;

function Server({ games, db, _clientInfo, _roomInfo }) {
export function Server({ games, db, _clientInfo, _roomInfo }) {
const app = new Koa();
const io = new IO({
ioOptions: {
Expand All @@ -28,7 +28,6 @@ function Server({ games, db, _clientInfo, _roomInfo }) {
if (db === undefined) {
db = new InMemory();
}
db.connect();

const clientInfo = _clientInfo || new Map();
const roomInfo = _roomInfo || new Map();
Expand Down Expand Up @@ -130,7 +129,11 @@ function Server({ games, db, _clientInfo, _roomInfo }) {
});
}

return app;
return {
app,
run: async (port, callback) => {
await db.connect();
app.listen(port, callback);
},
};
}

export default Server;
15 changes: 8 additions & 7 deletions src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://opensource.org/licenses/MIT.
*/

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

test('basic', () => {
const server = Server({ games: [game] });
server.run();
expect(server).not.toBe(undefined);
});

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

const server = Server({ games: [game], _clientInfo, _roomInfo });
const io = server.context.io;
const io = server.app.context.io;

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

test('sync', async () => {
const server = Server({ games: [game] });
const io = server.context.io;
const io = server.app.context.io;
expect(server).not.toBe(undefined);

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

test('action', async () => {
const server = Server({ games: [game] });
const io = server.context.io;
const io = server.app.context.io;
const action = ActionCreators.gameEvent('endTurn');

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

const server = Server({ games: [game] });
const io = server.context.io;
const io = server.app.context.io;

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

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

const game = Game({});
const server = Server({ games: [game], db: new Custom() });
const io = server.context.io;
const io = server.app.context.io;

await io.socket.receive('sync', 'gameID');
expect(getId).toBe('gameID');
Expand Down

0 comments on commit 557b66c

Please sign in to comment.