-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
29 lines (26 loc) · 715 Bytes
/
index.js
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
const express = require("express");
var http = require("http");
const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = require("socket.io")(server);
//middlewre
app.use(express.json());
var clients = {};
io.on("connection", (socket) => {
console.log("connetetd");
console.log(socket.id, "has joined");
socket.on("signin", (id) => {
console.log(id);
clients[id] = socket;
console.log(clients);
});
socket.on("message", (msg) => {
console.log(msg);
let targetId = msg.targetId;
if (clients[targetId]) clients[targetId].emit("message", msg);
});
});
server.listen(port, "0.0.0.0", () => {
console.log("server started");
});