-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
47 lines (40 loc) · 1.08 KB
/
App.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require("express");
const WebSocket = require("ws");
const http = require("http");
const DEVICES = [];
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended:true}));
const server = http.createServer(app);
const wss = new WebSocket.Server({server});
wss.on('connection',(ws)=>{
console.log("yeni bağlantı");
DEVICES.push(ws);
ws.on('message',(data)=>{
const json = JSON.parse(data);
const str = JSON.stringify(json);
console.log("message : "+str);
sendAll(str);
});
ws.on('close',()=>{
console.log("Kullanıcı çıktı");
const removeIndex = DEVICES.indexOf(ws);
DEVICES.splice(removeIndex,1);
})
});
const PORT = 8000;
server.listen(PORT,()=>{
console.log("Sunucu başladı");
})
const sendAll = (message)=>{
for (var i=0; i < DEVICES.length; i++) {
DEVICES[i].send(message);
}
}
app.get('/',(req,res)=>{
res.json("ÜMİT ÖZDAĞ REİSSSSSSSS");
});
app.get('/kullanicilar',(req,res)=>{
const num = DEVICES.length;
res.json(num);
});