init
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
PORT=5002
|
||||
MONGO_URI=mongodb://root:p62Z!ZatgY25@194.26.138.94:27017/
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "server",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/index.ts",
|
||||
"build": "npx tsc",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"mongoose": "^8.1.2",
|
||||
"socket.io": "^4.7.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.15",
|
||||
"@types/express": "^4.17.20",
|
||||
"@types/node": "^20.8.10",
|
||||
"nodemon": "^3.0.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.2.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { connect } from "mongoose";
|
||||
|
||||
async function connectDB() {
|
||||
try {
|
||||
await connect(process.env.MONGO_URI!, { dbName: "pixel_streaming" });
|
||||
console.log("MongoDB connected...");
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error(error.message);
|
||||
}
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
export default connectDB;
|
||||
@@ -0,0 +1,82 @@
|
||||
import { connect } from "mongoose";
|
||||
import "dotenv/config";
|
||||
import express, { json } from "express";
|
||||
import cors from "cors";
|
||||
import connectDB from "./config/db";
|
||||
import activeSessionRouter from "./routes/activeSession";
|
||||
import { Server } from "socket.io";
|
||||
import { createServer } from "http";
|
||||
|
||||
connectDB();
|
||||
|
||||
const app = express();
|
||||
const httpServer = createServer(app);
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.use(json());
|
||||
app.use(cors());
|
||||
|
||||
app.get("/", (_req, res) => {
|
||||
res.json({ ok: 1 });
|
||||
});
|
||||
|
||||
app.use("/activeSessions", activeSessionRouter);
|
||||
|
||||
const io = new Server(httpServer, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
},
|
||||
});
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
isAdmin: boolean;
|
||||
}
|
||||
|
||||
interface Message {
|
||||
name: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
interface Auth {
|
||||
roomId: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
const { roomId, user } = socket.handshake.auth as Auth;
|
||||
|
||||
if (!roomId || !user) return;
|
||||
|
||||
socket.data = { roomId, user };
|
||||
socket.join(roomId);
|
||||
|
||||
const sockets = await io.to(roomId).fetchSockets();
|
||||
const roomUsers = sockets.map((socket) => socket.handshake.auth.user);
|
||||
|
||||
socket.on("connection", () => {
|
||||
console.log("connect");
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
io.to(roomId).emit("update", roomUsers);
|
||||
}, 1);
|
||||
|
||||
socket.on("message", (message: Message) => {
|
||||
io.to(roomId).emit("message", message);
|
||||
});
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
const sockets = await io.to(roomId).fetchSockets();
|
||||
const roomUsers = sockets.map((socket) => socket.handshake.auth.user);
|
||||
|
||||
setTimeout(() => {
|
||||
io.to(roomId).emit("update", roomUsers);
|
||||
}, 1);
|
||||
});
|
||||
});
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.log(`Server is listening on port ${port}`);
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { model, Schema } from "mongoose";
|
||||
|
||||
const activeSessionSchema = new Schema(
|
||||
{
|
||||
location: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
server: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
uePid: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
cirrusPid: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
uePort: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
cirrusPort: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
ownerIp: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
connectedPlayersCount: {
|
||||
type: Number,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true },
|
||||
toObject: { virtuals: true },
|
||||
}
|
||||
);
|
||||
|
||||
const ActiveSession = model("Active_Session", activeSessionSchema);
|
||||
|
||||
export default ActiveSession;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Router } from "express";
|
||||
import ActiveSession from "../models/ActiveSession";
|
||||
import { isValidObjectId } from "mongoose";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
const activeSessionId = req.params.id;
|
||||
|
||||
if (!isValidObjectId(activeSessionId)) {
|
||||
return res.json({ status: "error", message: "Invalid ObjectId" });
|
||||
}
|
||||
|
||||
const activeSession = await ActiveSession.findById(activeSessionId);
|
||||
|
||||
res.json(activeSession);
|
||||
});
|
||||
|
||||
const activeSessionRouter = router;
|
||||
|
||||
export default activeSessionRouter;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Router } from "express";
|
||||
import { isValidObjectId } from "mongoose";
|
||||
import ActiveSession from "../models/ActiveSession";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
const activeSessionId = req.params.id;
|
||||
|
||||
if (!isValidObjectId(activeSessionId)) {
|
||||
return res.json({ status: "error", message: "Invalid ObjectId" });
|
||||
}
|
||||
|
||||
const activeSession = await ActiveSession.findById(activeSessionId);
|
||||
|
||||
res.json(activeSession);
|
||||
});
|
||||
|
||||
const roomRouter = router;
|
||||
|
||||
export default roomRouter;
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "CommonJS",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"ts-node": {
|
||||
"transpileOnly": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user