Skip to content

Commit

Permalink
Add port argument (#69)
Browse files Browse the repository at this point in the history
* Add port argument

* Fix codeclimate
  • Loading branch information
kostia-official authored and daffl committed Aug 21, 2018
1 parent 82aaa0f commit c3faab8
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/socketio/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ type FeatherSocketCallback =
(io: any) => void;

type FeathersSockeOptions =
number |
SocketIO.ServerOptions |
FeatherSocketCallback;


declare function feathersSocketIO(
port?: FeathersSockeOptions,
options?: FeathersSockeOptions,
config?: FeathersSockeOptions
): () => void;
Expand Down
12 changes: 9 additions & 3 deletions packages/socketio/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import socket from 'feathers-socket-commons';

const debug = makeDebug('feathers-socketio');

export default function (options, config) {
if (typeof options === 'function') {
export default function (port, options, config) {
if (typeof port !== 'number') {
config = options;
options = port;
port = null;
}

if (typeof options !== 'object') {
config = options;
options = {};
}
Expand All @@ -21,7 +27,7 @@ export default function (options, config) {
let io = this.io;

if (!io) {
io = this.io = socketio.listen(server, options);
io = this.io = socketio.listen(port || server, options);

io.use(function (socket, next) {
socket.feathers = { provider: 'socketio' };
Expand Down
109 changes: 109 additions & 0 deletions packages/socketio/test/arguments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import feathers from 'feathers';
import io from 'socket.io-client';
import socketio from '../src';

const createUniquePort = UniquePortCreator(3000);

describe('Arguments', function () {
const options = { path: '/ws/' };
const callback = (io) => io.on('connection', (socket) => {
socket.emit('event');
});

describe('No arguments', function () {
const port = createUniquePort();
const app = feathers().configure(socketio());
const socket = io('http://localhost:' + port);

it('should be connected', (done) => {
serverListen(app, port, () => {
socket.on('connect', done);
});
});
});

describe('Pass options', function () {
const port = createUniquePort();
const app = feathers().configure(socketio(options));
const socket = io('http://localhost:' + port, options);

it('should be connected', (done) => {
serverListen(app, port, () => {
socket.on('connect', done);
});
});
});

describe('Pass callback', function () {
const port = createUniquePort();
const app = feathers().configure(socketio(callback));
const socket = io('http://localhost:' + port);

it('should be connected', (done) => {
serverListen(app, port, () => {
socket.once('event', done);
});
});
});

describe('Pass options and callback', function () {
const port = createUniquePort();
const app = feathers().configure(socketio(options, callback));
const socket = io('http://localhost:' + port, options);

it('should be connected', (done) => {
serverListen(app, port, () => {
socket.once('event', done);
});
});
});

describe('Pass port and options', function () {
const socketPort = createUniquePort();
const appPort = createUniquePort();
const app = feathers().configure(socketio(socketPort, options));
const socket = io('http://localhost:' + socketPort, options);

it('should be connected', (done) => {
serverListen(app, appPort, () => {
socket.on('connect', done);
});
});
});

describe('Pass port and callback', function () {
const socketPort = createUniquePort();
const appPort = createUniquePort();
const app = feathers().configure(socketio(socketPort, callback));
const socket = io('http://localhost:' + socketPort);

it('should be connected', (done) => {
serverListen(app, appPort, () => {
socket.once('event', done);
});
});
});

describe('Pass port, options and callback', function () {
const socketPort = createUniquePort();
const appPort = createUniquePort();
const app = feathers().configure(socketio(socketPort, options, callback));
const socket = io('http://localhost:' + socketPort, options);

it('should be connected', (done) => {
serverListen(app, appPort, () => {
socket.once('event', done);
});
});
});
});

function UniquePortCreator (from) {
return () => from++;
}

function serverListen (app, port, cb) {
let server = app.listen(port);
app.setup(server);
server.on('listening', cb);
}

0 comments on commit c3faab8

Please sign in to comment.