93 lines
2.5 KiB
Plaintext
93 lines
2.5 KiB
Plaintext
import express, { Request, Response } from 'express';
|
|
import http from 'http'; // Import Node's built-in HTTP module
|
|
import { Server as SocketServer } from 'socket.io'; // Import Socket.io
|
|
// import path from 'path';
|
|
// import dotenv from 'dotenv';
|
|
require('dotenv').config();
|
|
// import { env } from 'node:process';
|
|
|
|
|
|
|
|
// import dotenv from 'dotenv';
|
|
// import { defaultRoute } from './routes';
|
|
|
|
// import http from 'http';
|
|
// import { Server as SocketServer } from 'socket.io';
|
|
|
|
|
|
// dotenv.config();
|
|
|
|
const app = express();
|
|
const server = http.createServer(app); // Create an HTTP server
|
|
const io = new SocketServer(server); // Create a Socket.io server instance
|
|
|
|
// // Serve static files (optional)
|
|
// app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// Middleware to parse JSON
|
|
app.use(express.json());
|
|
|
|
// // Sample data
|
|
// const users: { id: number; name: string }[] = [
|
|
// { id: 1, name: 'Alice' },
|
|
// { id: 2, name: 'Bob' },
|
|
// ];
|
|
|
|
// // Define a route to get all users
|
|
// app.get('/users', (req: Request, res: Response) => {
|
|
// res.json(users);
|
|
// });
|
|
|
|
|
|
app.get('/send_notification', (req: Request, res: Response) => {
|
|
// io.on('connection', (socket) => {
|
|
// console.log('A user connected get');
|
|
console.log(req);
|
|
res.json([])
|
|
// io.timeout(2000).emit("message", "fuck this", (err: any, responses: Response) => {
|
|
// if (err) {
|
|
// console.log("#2");
|
|
|
|
// console.log(err);
|
|
|
|
// // some clients did not acknowledge the event in the given delay
|
|
// } else {
|
|
// console.log("#1");
|
|
|
|
// console.log(responses); // one response per client
|
|
// }
|
|
// });
|
|
// })
|
|
// res.json(users);
|
|
});
|
|
|
|
// ... (Previous route definitions)
|
|
|
|
// Socket.io setup
|
|
io.on('connection', (socket) => {
|
|
// console.log(socket.handshake.auth.token);
|
|
|
|
// console.log('A user connected');
|
|
|
|
// Example: Send a message to the connected client
|
|
// socket.emit('message', 'Welcome to the chat!');
|
|
|
|
// Handle client disconnection
|
|
// socket.on('disconnect', () => {
|
|
// console.log('A user disconnected');
|
|
// });
|
|
|
|
// // Handle custom events
|
|
// socket.on('chatMessage', (message) => {
|
|
// // Broadcast the message to all connected clients
|
|
// console.log(socket.handshake.auth.token);
|
|
|
|
// io.emit('chatMessage', message);
|
|
// });
|
|
});
|
|
|
|
// Start the HTTP server
|
|
server.listen(process.env.port, () => {
|
|
console.log(`Server is running on port ${process.env.port}`);
|
|
});
|