Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Add port argument #69

Merged
merged 2 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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 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 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);
}