UNPKG

3.04 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const http_1 = __importDefault(require("http"));
6const socket_io_1 = require("socket.io");
7const commons_1 = require("@feathersjs/commons");
8const transport_commons_1 = require("@feathersjs/transport-commons");
9const middleware_1 = require("./middleware");
10const debug = (0, commons_1.createDebug)('@feathersjs/socketio');
11function configureSocketio(port, options, config) {
12 if (typeof port !== 'number') {
13 config = options;
14 options = port;
15 port = null;
16 }
17 if (typeof options !== 'object') {
18 config = options;
19 options = {};
20 }
21 return (app) => {
22 // Function that gets the connection
23 const getParams = (socket) => socket.feathers;
24 // A mapping from connection to socket instance
25 const socketMap = new WeakMap();
26 // Promise that resolves with the Socket.io `io` instance
27 // when `setup` has been called (with a server)
28 const done = new Promise((resolve) => {
29 const { listen, setup } = app;
30 Object.assign(app, {
31 async listen(...args) {
32 if (typeof listen === 'function') {
33 // If `listen` already exists
34 // usually the case when the app has been expressified
35 return listen.call(this, ...args);
36 }
37 const server = http_1.default.createServer();
38 await this.setup(server);
39 return server.listen(...args);
40 },
41 async setup(server, ...rest) {
42 if (!this.io) {
43 const io = (this.io = new socket_io_1.Server(port || server, options));
44 io.use((0, middleware_1.disconnect)(app, getParams, socketMap));
45 io.use((0, middleware_1.params)(app, socketMap));
46 io.use((0, middleware_1.authentication)(app, getParams));
47 // In Feathers it is easy to hit the standard Node warning limit
48 // of event listeners (e.g. by registering 10 services).
49 // So we set it to a higher number. 64 should be enough for everyone.
50 io.sockets.setMaxListeners(64);
51 }
52 if (typeof config === 'function') {
53 debug('Calling SocketIO configuration function');
54 config.call(this, this.io);
55 }
56 resolve(this.io);
57 return setup.call(this, server, ...rest);
58 }
59 });
60 });
61 app.configure((0, transport_commons_1.socket)({
62 done,
63 socketMap,
64 getParams,
65 emit: 'emit'
66 }));
67 };
68}
69module.exports = configureSocketio;
70//# sourceMappingURL=index.js.map
\No newline at end of file