-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (29 loc) · 875 Bytes
/
server.js
File metadata and controls
36 lines (29 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const express = require('express')
const http = require('http')
const {Server} = require('socket.io')
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
}
});
app.use(express.static(path.join(__dirname, "public")));
let chatHistory = [];
io.on("connection", (socket) => {
console.log("User Connected");
socket.emit("history", chatHistory);
socket.on("message", (text) => {
console.log(`Message sent: ${text.text}`);
chatHistory.push(text);
if(chatHistory.length > 100){
chatHistory.shift();
}
socket.broadcast.emit("message", text);
});
});
const port = process.env.PORT || 3000;
server.listen(port, () =>{
console.log("http://localhost:8080/");
});