wss-autoload
install
npm i wss-autoload
options
dir
:
options
:If you want to pass some custom options to the registered plugins via fastify-autoload, use the options
key
ignorePattern
:If you have some files in the folder that you'd like autoload to skip you can set ignorePattern
option to a regex. If that matches a file it will not load it.
includeTypeScript
:If you are using TypeScript and something like ts-node to load the .ts files directly you can set includeTypeScript
option to true. This will load plugins from .ts files as well as .js files.
usage
import { join } from 'path';
import { createServer } from 'http';
import { Server } from 'wssio';
import { namespaceAutoload } from 'wss-autoload';
import { namespacePlugin } from 'wss-plugin';
const server = createServer();
const wssio = new Server({
server,
});
const nsp = wssio.of('/test');
// 注册关闭钩子函数、以达到关闭服务
nsp.register(namespacePlugin((namespace, _opts, done) => {
namespace.addHook('onClose', () => {
wssio.close();
server.close();
});
done();
}, {
name: 'onClose',
}));
nsp.register(namespaceAutoload, {
dir:path.join(__dirname, 'foo')
}).ready((err)=>{
nsp.$log.info('wss ready.....');
nsp.$log.error(err);
server.listen(3000, '0.0.0.0');
});