💿 Created a socket mixin
This commit is contained in:
@@ -1,5 +1,16 @@
|
|||||||
import { Server } from "socket.io";
|
import { Server } from "socket.io";
|
||||||
|
import { createServer } from "http";
|
||||||
|
|
||||||
export const SocketIOMixin = () => {
|
export const SocketIOMixin = () => {
|
||||||
|
const socketServer = createServer();
|
||||||
|
socketServer.listen(3001, `0.0.0.0`);
|
||||||
|
const socketIOConnection = new Server(socketServer, {
|
||||||
|
cors: {
|
||||||
|
origin: "*",
|
||||||
|
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
||||||
|
preflightContinue: false,
|
||||||
|
optionsSuccessStatus: 204,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return socketIOConnection;
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,8 @@ import path from "path";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings";
|
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings";
|
||||||
import { createServer } from "http";
|
import { createServer } from "http";
|
||||||
import { Server } from "socket.io";
|
import { Server, Socket } from "socket.io";
|
||||||
|
import { SocketIOMixin } from "../mixins/socket.io.mixin";
|
||||||
const socketServer = createServer();
|
|
||||||
const SOCKET_HOST = process.env.DOCKER_HOST || `localhost`;
|
const SOCKET_HOST = process.env.DOCKER_HOST || `localhost`;
|
||||||
export default class ApiService extends Service {
|
export default class ApiService extends Service {
|
||||||
public constructor(broker: ServiceBroker) {
|
public constructor(broker: ServiceBroker) {
|
||||||
@@ -15,7 +14,7 @@ export default class ApiService extends Service {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.parseServiceSchema({
|
this.parseServiceSchema({
|
||||||
name: "api",
|
name: "api",
|
||||||
mixins: [ApiGateway],
|
mixins: [ApiGateway, SocketIOMixin],
|
||||||
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
|
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
|
||||||
settings: {
|
settings: {
|
||||||
port: process.env.PORT || 3000,
|
port: process.env.PORT || 3000,
|
||||||
@@ -94,16 +93,7 @@ export default class ApiService extends Service {
|
|||||||
methods: {},
|
methods: {},
|
||||||
started(): any {
|
started(): any {
|
||||||
// Socket gateway-ish
|
// Socket gateway-ish
|
||||||
// Create a Socket.IO instance, passing it our server
|
this.io = SocketIOMixin();
|
||||||
socketServer.listen(3001, `0.0.0.0`);
|
|
||||||
this.io = new Server(socketServer, {
|
|
||||||
cors: {
|
|
||||||
origin: "*",
|
|
||||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
|
||||||
preflightContinue: false,
|
|
||||||
optionsSuccessStatus: 204,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// Add a connect listener
|
// Add a connect listener
|
||||||
this.io.on("connection", (client) => {
|
this.io.on("connection", (client) => {
|
||||||
console.log("Client connected via websocket!");
|
console.log("Client connected via websocket!");
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ export default class LibraryQueueService extends Service {
|
|||||||
},
|
},
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
dbImportResult,
|
dbImportResult,
|
||||||
|
|||||||
Reference in New Issue
Block a user