Skip to content

Commit

Permalink
Added ability to specify server protocol (#247)
Browse files Browse the repository at this point in the history
* Added ability to specify server protocol

* Fixed protocol check would accept an occurance anywhere in server string

* Replaced var with let

* tighter regex

* update docs
  • Loading branch information
jorbascrumps authored and nicolodavis committed Aug 4, 2018
1 parent 268aab8 commit fe8a9d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
10 changes: 10 additions & 0 deletions docs/multiplayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ const App = Client({
});
```

You may also specify a protocol here (if you want to use SSL, for example):

```js
const App = Client({
game: TicTacToe,
board: TicTacToeBoard,
multiplayer: { server: 'https://localhost:8000/' },
});
```

#### Associating Clients with Players

Clients needs to be associated with a particular player in order
Expand Down
10 changes: 6 additions & 4 deletions src/client/multiplayer/multiplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ export class Multiplayer {
connect() {
if (!this.socket) {
if (this.server) {
this.socket = io(
'http://' + this.server + '/' + this.gameName,
this.socketOpts
);
let server = this.server;
if (server.search(/^https?:\/\//) == -1) {
server = 'http://' + this.server;
}

this.socket = io(server + '/' + this.gameName, this.socketOpts);
} else {
this.socket = io('/' + this.gameName, this.socketOpts);
}
Expand Down
47 changes: 35 additions & 12 deletions src/client/multiplayer/multiplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,43 @@ test('move blacklist', () => {
mockSocket.emit.mockReset();
});

test('game server is set when provided', () => {
var hostname = 'host';
var port = '1234';
var server = hostname + ':' + port;
describe('server option', () => {
const hostname = 'host';
const port = '1234';

test('without protocol', () => {
const server = hostname + ':' + port;
const m = new Multiplayer({ server });
m.connect();
expect(m.socket.io.engine.hostname).toEqual(hostname);
expect(m.socket.io.engine.port).toEqual(port);
expect(m.socket.io.engine.secure).toEqual(false);
});

const m = new Multiplayer({ server });
m.connect();
expect(m.socket.io.engine.hostname).toEqual(hostname);
expect(m.socket.io.engine.port).toEqual(port);
test('https', () => {
const serverWithProtocol = 'https://' + hostname + ':' + port + '/';
const m = new Multiplayer({ server: serverWithProtocol });
m.connect();
expect(m.socket.io.engine.hostname).toEqual(hostname);
expect(m.socket.io.engine.port).toEqual(port);
expect(m.socket.io.engine.secure).toEqual(true);
});

const m2 = new Multiplayer();
m2.connect();
expect(m2.socket.io.engine.hostname).not.toEqual(hostname);
expect(m2.socket.io.engine.port).not.toEqual(port);
test('http', () => {
const serverWithProtocol = 'http://' + hostname + ':' + port + '/';
const m = new Multiplayer({ server: serverWithProtocol });
m.connect();
expect(m.socket.io.engine.hostname).toEqual(hostname);
expect(m.socket.io.engine.port).toEqual(port);
expect(m.socket.io.engine.secure).toEqual(false);
});

test('no server set', () => {
const m = new Multiplayer();
m.connect();
expect(m.socket.io.engine.hostname).not.toEqual(hostname);
expect(m.socket.io.engine.port).not.toEqual(port);
});
});

test('game server accepts enhanced store', () => {
Expand Down

0 comments on commit fe8a9d0

Please sign in to comment.