-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (43 loc) · 1.04 KB
/
server.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
/*
* @Author: sunheng
* @Date: 2022-12-21 15:53:45
* @LastEditors: sunheng
* @LastEditTime: 2022-12-22 13:47:37
* @Description: 请填写简介
*/
const express = require("express");
const http = require("http");
const cors = require("cors");
// chushihua
const app = express();
const server = http.createServer(app);
app.use(cors());
//初始化io
const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
// 服务器socket链接
io.on("connection", (socket) => {
socket.emit("me", socket.id);
socket.on("disconnect", () => {
socket.broadcast.emit("CallEnded");
});
socket.on("callUser", (data) => {
io.to(data.idToCall).emit("callUser", {
signal: data.signalData,
from: data.from,
name: data.name,
});
});
socket.on("answerCall", (data) => {
//将数据传递给通信的发起方
console.log(data);
io.to(data.to).emit("callAccepted", data.signal);
});
});
server.listen(5000, () => {
console.log("我在运行服务器,端口号为5000");
});