Changes to socket.io code
This commit is contained in:
11574
package-lock.json
generated
11574
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,12 +4,11 @@ import path from "path";
|
|||||||
import { Service, ServiceBroker, Context } from "moleculer";
|
import { Service, ServiceBroker, Context } from "moleculer";
|
||||||
import ApiGateway from "moleculer-web";
|
import ApiGateway from "moleculer-web";
|
||||||
import { getCovers, extractArchive } from "../utils/uncompression.utils";
|
import { getCovers, extractArchive } from "../utils/uncompression.utils";
|
||||||
import { map } from "lodash";
|
import { map, flatten } from "lodash";
|
||||||
import JSONStream from "JSONStream";
|
import JSONStream from "JSONStream";
|
||||||
const IO = require("socket.io")();
|
const IO = require("socket.io")();
|
||||||
|
|
||||||
export default class ApiService extends Service {
|
export default class ApiService extends Service {
|
||||||
|
|
||||||
public constructor(broker: ServiceBroker) {
|
public constructor(broker: ServiceBroker) {
|
||||||
super(broker);
|
super(broker);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -20,65 +19,78 @@ export default class ApiService extends Service {
|
|||||||
settings: {
|
settings: {
|
||||||
port: process.env.PORT || 3000,
|
port: process.env.PORT || 3000,
|
||||||
|
|
||||||
routes: [{
|
routes: [
|
||||||
path: "/api",
|
{
|
||||||
whitelist: [
|
path: "/api",
|
||||||
// Access to any actions in all services under "/api" URL
|
whitelist: [
|
||||||
"**",
|
// Access to any actions in all services under "/api" URL
|
||||||
],
|
"**",
|
||||||
use: [],
|
],
|
||||||
mergeParams: true,
|
use: [],
|
||||||
autoAliases: true,
|
mergeParams: true,
|
||||||
|
autoAliases: true,
|
||||||
|
|
||||||
aliases: {},
|
aliases: {
|
||||||
/**
|
async "POST getComicCovers"(req, res) {
|
||||||
* Before call hook. You can check the request.
|
console.log(req.body);
|
||||||
* @param {Context} ctx
|
try {
|
||||||
* @param {Object} route
|
const { extractionOptions, walkedFolders } =
|
||||||
* @param {IncomingMessage} req
|
req.body;
|
||||||
* @param {ServerResponse} res
|
switch (extractionOptions.extractionMode) {
|
||||||
* @param {Object} data
|
case "bulk":
|
||||||
onBeforeCall(ctx: Context<any,{userAgent: string}>,
|
const extractedDataPromises = map(
|
||||||
route: object, req: IncomingMessage, res: ServerResponse) {
|
walkedFolders,
|
||||||
Set request headers to context meta
|
async (folder) =>
|
||||||
ctx.meta.userAgent = req.headers["user-agent"];
|
await extractArchive(
|
||||||
},
|
extractionOptions,
|
||||||
*/
|
folder
|
||||||
|
)
|
||||||
/**
|
);
|
||||||
* After call hook. You can modify the data.
|
return Promise.all(
|
||||||
* @param {Context} ctx
|
extractedDataPromises
|
||||||
* @param {Object} route
|
).then((data) => flatten(data));
|
||||||
* @param {IncomingMessage} req
|
case "single":
|
||||||
* @param {ServerResponse} res
|
return await extractArchive(
|
||||||
* @param {Object} data
|
extractionOptions,
|
||||||
*
|
walkedFolders[0]
|
||||||
onAfterCall(ctx: Context, route: object, req: IncomingMessage, res: ServerResponse, data: object) {
|
);
|
||||||
// Async function which return with Promise
|
default:
|
||||||
return doSomething(ctx, res, data);
|
console.log(
|
||||||
},
|
"Unknown extraction mode selected."
|
||||||
*/
|
);
|
||||||
|
return {
|
||||||
// Calling options. More info: https://moleculer.services/docs/0.14/moleculer-web.html#Calling-options
|
message:
|
||||||
callingOptions: {},
|
"Unknown extraction mode selected.",
|
||||||
|
errorCode: "90",
|
||||||
bodyParsers: {
|
data: `${extractionOptions}`,
|
||||||
json: {
|
};
|
||||||
strict: false,
|
}
|
||||||
limit: "1MB",
|
res.end();
|
||||||
|
} catch (error) {}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
urlencoded: {
|
|
||||||
extended: true,
|
// Calling options. More info: https://moleculer.services/docs/0.14/moleculer-web.html#Calling-options
|
||||||
limit: "1MB",
|
callingOptions: {},
|
||||||
|
|
||||||
|
bodyParsers: {
|
||||||
|
json: {
|
||||||
|
strict: false,
|
||||||
|
limit: "1MB",
|
||||||
|
},
|
||||||
|
urlencoded: {
|
||||||
|
extended: true,
|
||||||
|
limit: "1MB",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Mapping policy setting. More info: https://moleculer.services/docs/0.14/moleculer-web.html#Mapping-policy
|
||||||
|
mappingPolicy: "all", // Available values: "all", "restrict"
|
||||||
|
|
||||||
|
// Enable/disable logging
|
||||||
|
logging: true,
|
||||||
},
|
},
|
||||||
|
],
|
||||||
// Mapping policy setting. More info: https://moleculer.services/docs/0.14/moleculer-web.html#Mapping-policy
|
|
||||||
mappingPolicy: "all", // Available values: "all", "restrict"
|
|
||||||
|
|
||||||
// Enable/disable logging
|
|
||||||
logging: true,
|
|
||||||
}],
|
|
||||||
// Do not log client side errors (does not log an error response when the error.code is 400<=X<500)
|
// Do not log client side errors (does not log an error response when the error.code is 400<=X<500)
|
||||||
log4XXResponses: false,
|
log4XXResponses: false,
|
||||||
// Logging the request parameters. Set to any log level to enable it. E.g. "info"
|
// Logging the request parameters. Set to any log level to enable it. E.g. "info"
|
||||||
@@ -93,23 +105,46 @@ export default class ApiService extends Service {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {},
|
||||||
|
|
||||||
|
|
||||||
},
|
|
||||||
events: {
|
events: {
|
||||||
"**"(payload, sender, event) {
|
"**"(payload, sender, event) {
|
||||||
if (this.io)
|
if (this.io)
|
||||||
this.io.emit("event", {
|
this.io.emit("event", {
|
||||||
sender,
|
sender,
|
||||||
event,
|
event,
|
||||||
payload
|
payload,
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
|
started(): any {
|
||||||
|
// Create a Socket.IO instance, passing it our server
|
||||||
|
this.io = IO.listen(this.server);
|
||||||
|
|
||||||
|
// Add a connect listener
|
||||||
|
this.io.on("connection", (client) => {
|
||||||
|
this.logger.info("Client connected via websocket!");
|
||||||
|
|
||||||
|
client.on("call", ({ action, params, opts }, done) => {
|
||||||
|
this.logger.info(
|
||||||
|
"Received request from client! Action:",
|
||||||
|
action,
|
||||||
|
", Params:",
|
||||||
|
params
|
||||||
|
);
|
||||||
|
|
||||||
|
this.broker
|
||||||
|
.call(action, params, opts)
|
||||||
|
.then((res) => {
|
||||||
|
if (done) done(res);
|
||||||
|
})
|
||||||
|
.catch((err) => this.logger.error(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("disconnect", () => {
|
||||||
|
this.logger.info("Client disconnected");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import through2 from "through2";
|
|||||||
import oboe from "oboe";
|
import oboe from "oboe";
|
||||||
import H from "highland";
|
import H from "highland";
|
||||||
import { stringify } from "highland-json";
|
import { stringify } from "highland-json";
|
||||||
|
const IO = require("socket.io")();
|
||||||
|
|
||||||
export default class ProductsService extends Service {
|
export default class ProductsService extends Service {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -54,32 +55,6 @@ export default class ProductsService extends Service {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {},
|
methods: {},
|
||||||
started(): any {
|
|
||||||
// Create a Socket.IO instance, passing it our server
|
|
||||||
this.io = IO.listen(this.server);
|
|
||||||
|
|
||||||
// Add a connect listener
|
|
||||||
this.io.on("connection", client => {
|
|
||||||
this.logger.info("Client connected via websocket!");
|
|
||||||
|
|
||||||
client.on("call", ({ action, params, opts }, done) => {
|
|
||||||
this.logger.info("Received request from client! Action:", action, ", Params:", params);
|
|
||||||
|
|
||||||
this.broker.call(action, params, opts)
|
|
||||||
.then(res => {
|
|
||||||
if (done)
|
|
||||||
done(res);
|
|
||||||
})
|
|
||||||
.catch(err => this.logger.error(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on("disconnect", () => {
|
|
||||||
this.logger.info("Client disconnected");
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
},
|
||||||
schema
|
schema
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user