Socekt server
This commit is contained in:
1 parent
c751f4c5e7
commit
7b70be5e6e
11 files changed
+1201
-52
No files matched your search
@@ -0,0 +1,41 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.*
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/versions
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
/.next/
|
||||
/out/
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# env files (can opt-in for committing if needed)
|
||||
.env*
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
@@ -0,0 +1,35 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const http = require("http");
|
||||
const server = http.createServer(app);
|
||||
|
||||
const { Server } = require("socket.io");
|
||||
const io = new Server(server, {
|
||||
cors: {
|
||||
origin: "http://192.168.0.131:3000", // Replace with the origin of your client application
|
||||
methods: ["GET", "POST", "WS"], // Allowed HTTP methods
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
// res.sendFile(__dirname + "/index.html");
|
||||
res.send(`Upgrade`);
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
console.log("A user connected");
|
||||
|
||||
// Handle custom events from the client
|
||||
socket.on("chat message", (msg) => {
|
||||
io.emit("chat message", msg); // Emit the message to all connected clients
|
||||
});
|
||||
|
||||
// Handle disconnections
|
||||
socket.on("disconnect", () => {
|
||||
console.log("User disconnected");
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(4000, () => {
|
||||
console.log("Listening on port 4000");
|
||||
});
|
||||
Generated
+1076
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"express": "^5.2.1",
|
||||
"socket.io": "^4.8.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "main": "index.js", "output": "socket-server" }
|
||||
Reference in new issue
Block a user