mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +00:00
migrated socket IO initialization to typescript
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
let io;
|
||||
|
||||
module.exports = {
|
||||
init: httpServer => {
|
||||
io = require("socket.io")(httpServer);
|
||||
return io;
|
||||
},
|
||||
getIO: () => {
|
||||
if (!io) {
|
||||
throw new Error("Socket IO not initialized");
|
||||
}
|
||||
return io;
|
||||
},
|
||||
};
|
||||
15
backend/src/libs/socket.ts
Normal file
15
backend/src/libs/socket.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import socketIo, { Server as SocketIO } from "socket.io";
|
||||
import { Server } from "http";
|
||||
|
||||
let io: SocketIO;
|
||||
|
||||
export const initIO = (httpServer: Server): SocketIO => {
|
||||
io = socketIo(httpServer);
|
||||
return io;
|
||||
};
|
||||
export const getIO = (): SocketIO => {
|
||||
if (!io) {
|
||||
throw new Error("Socket IO not initialized");
|
||||
}
|
||||
return io;
|
||||
};
|
||||
@@ -4,11 +4,12 @@ import "express-async-errors";
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import cors from "cors";
|
||||
import multer from "multer";
|
||||
import Sentry from "@sentry/node";
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
import uploadConfig from "./config/upload";
|
||||
import AppError from "./errors/AppError";
|
||||
import routes from "./routes";
|
||||
import { initIO } from "./libs/socket";
|
||||
import "./database";
|
||||
|
||||
// import path from "path";
|
||||
@@ -43,23 +44,23 @@ const server = app.listen(process.env.PORT, () => {
|
||||
console.log(`Server started on port: ${process.env.PORT}`);
|
||||
});
|
||||
|
||||
// const io = require("./libs/socket").init(server);
|
||||
// io.on("connection", socket => {
|
||||
// console.log("Client Connected");
|
||||
// socket.on("joinChatBox", ticketId => {
|
||||
// console.log("A client joined a ticket channel");
|
||||
// socket.join(ticketId);
|
||||
// });
|
||||
const io = initIO(server);
|
||||
io.on("connection", socket => {
|
||||
console.log("Client Connected");
|
||||
socket.on("joinChatBox", ticketId => {
|
||||
console.log("A client joined a ticket channel");
|
||||
socket.join(ticketId);
|
||||
});
|
||||
|
||||
// socket.on("joinNotification", () => {
|
||||
// console.log("A client joined notification channel");
|
||||
// socket.join("notification");
|
||||
// });
|
||||
socket.on("joinNotification", () => {
|
||||
console.log("A client joined notification channel");
|
||||
socket.join("notification");
|
||||
});
|
||||
|
||||
// socket.on("disconnect", () => {
|
||||
// console.log("Client disconnected");
|
||||
// });
|
||||
// });
|
||||
socket.on("disconnect", () => {
|
||||
console.log("Client disconnected");
|
||||
});
|
||||
});
|
||||
|
||||
// const startWhatsAppSessions = async () => {
|
||||
// const whatsapps = await Whatsapp.findAll();
|
||||
|
||||
Reference in New Issue
Block a user