Skip to content

Commit

Permalink
Merge 4c00e03 into dbba255
Browse files Browse the repository at this point in the history
  • Loading branch information
chasen committed Jul 7, 2021
2 parents dbba255 + 4c00e03 commit 5b04191
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 266 deletions.
23 changes: 17 additions & 6 deletions docs/documentation/api/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,31 @@ be configured to run on a separate port.

A config object with the following options:

1. `games` (_array_): a list of game implementations
1. `games` (_array_) (required): a list of game implementations
(each should be an object conforming to the [Game API](/api/Game.md)).

2. `db` (_object_): the [database connector](/storage).
2. `origins` (_array_) (required): a list of allowed origins for
[CORS](https://a.lu "Cross-Origin Resource Sharing").

The list can contain strings or regular expressions, matching the origins
that are allowed to access the game server. For example, this could be
`['https://example.com']` if that’s where your game is running. While
developing locally you could set it to `[/localhost:\d+/]` to allow access for
any page running on localhost.

[cors]: https://github.com/expressjs/cors#configuration-options

3. `db` (_object_): the [database connector](/storage).
If not provided, an in-memory implementation is used.

3. `transport` (_object_): the transport implementation.
4. `transport` (_object_): the transport implementation.
If not provided, socket.io is used.

4. `uuid` (_function_): an optional function that returns a unique identifier, used to create new game IDs and — if `generateCredentials` is not specified — player credentials. Defaults to [nanoid](https://www.npmjs.com/package/nanoid).
5. `uuid` (_function_): an optional function that returns a unique identifier, used to create new game IDs and — if `generateCredentials` is not specified — player credentials. Defaults to [nanoid](https://www.npmjs.com/package/nanoid).

5. `generateCredentials` (_function_): an optional function that returns player credentials to store in the game metadata and validate against. If not specified, the `uuid` function will be used.
6. `generateCredentials` (_function_): an optional function that returns player credentials to store in the game metadata and validate against. If not specified, the `uuid` function will be used.

6. `authenticateCredentials` (_function_): an optional function that tests if a player’s move is made with the correct credentials when using the default socket.io transport implementation.
7. `authenticateCredentials` (_function_): an optional function that tests if a player’s move is made with the correct credentials when using the default socket.io transport implementation.

#### Returns

Expand Down
17 changes: 14 additions & 3 deletions docs/documentation/multiplayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,30 @@ a server at the location specified, which is discussed below.
### Setting up the server

We’ll create a new file at `src/server.js` to write our server code.
In order to run the game master on a Node server, we import the
boardgame.io server module and provide it with our `TicTacToe` game object.

boardgame.io provides a server module that simplifies running the game
master on a Node server. We import that module and configure it with our
`TicTacToe` game object and a list of URL origins we want to allow to
connect to the server. Later you would set `origins` with your game’s domain
name, but for now we’ll use a value that allows any locally served page
to connect.

```js
// src/server.js
const { Server } = require('boardgame.io/server');
const { TicTacToe } = require('./Game');
const server = Server({ games: [TicTacToe] });
const server = Server({
games: [TicTacToe],
origins: [/localhost:\d+/],
});
server.run(8000);
```

?> See [the Server reference page](api/Server.md) for more detail on
the various configuration options.

Because `Game.js` is an ES module, we will use [esm](https://github.com/standard-things/esm)
which enables us to use `import` statements in a Node environment:

Expand Down
5 changes: 3 additions & 2 deletions examples/react-web/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import TicTacToe from './src/tic-tac-toe/game';
import Chess from './src/chess/game';

const PORT = process.env.PORT || 8000;
const server = Server({ games: [TicTacToe, Chess] });
const origins = [`http://localhost:${PORT}`];
const server = Server({ games: [TicTacToe, Chess], origins });
server.run(PORT, () => {
console.log(`Serving at: http://localhost:${PORT}/`);
console.log(`Serving at: ${origins[0]}`);
});
Loading

0 comments on commit 5b04191

Please sign in to comment.