1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const http_1 = __importDefault(require("http"));
|
6 | const socket_io_1 = require("socket.io");
|
7 | const commons_1 = require("@feathersjs/commons");
|
8 | const transport_commons_1 = require("@feathersjs/transport-commons");
|
9 | const middleware_1 = require("./middleware");
|
10 | const debug = (0, commons_1.createDebug)('@feathersjs/socketio');
|
11 | function 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 |
|
23 | const getParams = (socket) => socket.feathers;
|
24 |
|
25 | const socketMap = new WeakMap();
|
26 |
|
27 |
|
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 |
|
34 |
|
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 |
|
48 |
|
49 |
|
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 | }
|
69 | module.exports = configureSocketio;
|
70 |
|
\ | No newline at end of file |