-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (44 loc) · 2.04 KB
/
server.js
File metadata and controls
64 lines (44 loc) · 2.04 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Modüllerin içe aktarılması
import express from "express";
import http from "http";
import { Server } from "socket.io";
import cors from "cors";
const app = express(); // express modülü kullanarak app yani uygulama oluşturulması.
const server = http.createServer(app); // app uygulamasının HTTP sunucusuna dönüştürülmesi
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
app.use(cors());
//Set-up server
app.get("/", (req, res) => { // express get ve send metodu
try {
res.status(200).send("Server is running"); // if request success return successful response to client
} catch (error) {
res.status(500).send("Internal Server Error"); // if request error return internal server error to client
}
});
//socket.io - Socket aracılığıyla bağlantı oluşturma
io.on("connection", (socket) => { //io socketten objemiz | eventname,(..arg listener) ev — Name of the event,@param listener — Callback function
//console.log(socket.id,"connected");
socket.emit("me", socket.id); // me bc i joined thats my specifies user
socket.on("disconnect", () => {
socket.broadcast.emit("callEnded") //simply broadcast meassage bu sokete bağlı clientların disconnet oluna bu mesaj hepsine gitsin istiyorum.
});
socket.on("callUser", ({ userToCall, signalData, from, name }) => { // Kullanıcıyı arayabilme yeteneği
io.to(userToCall).emit("callUser", {signal:signalData, from, name}) // Destructuring data from received client side. usertocall user idsi.
});
socket.on("answerCall", (data) => {
io.to(data.to).emit("callAccepted", data.signal)
});
socket.on("declineCall", (data) => {
io.to(data.to).emit("callDeclined",{name:data.name});
});
})
// Bağlantı noktası(port) belirleme ve sunucuyu başlatma
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Express server listening on port http://localhost:${port}`);
});