-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (62 loc) · 2.48 KB
/
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
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
64
65
66
67
68
69
70
71
72
73
var express = require("express");
var app = express();
const fs = require('fs');
let sslOptions = {
key: fs.readFileSync('C:/privkey.key'),//私钥
cert: fs.readFileSync('C:/cacert.pem')//证书
};
var https = require('https').createServer(sslOptions, app);
var io = require('socket.io')(https);
//收到get请求则发送html文档
app.get('/test', (req, res) => {
res.sendFile(__dirname + '/test.html');
})
//使用静态文件夹,连通css/js
app.use(express.static('public'));
//开启服务器
var server = https.listen(3000, () => {
var host = server.address().address == "::" ? "127.0.0.1" : server.address().port;
var port = server.address().port;
var path = "/test";
console.log("访问地址为 https://%s:%s%s", host, port, path);
});
io.on('connection', (socket) => {//连接加入子房间
socket.join( socket.id );
console.log("a user connected " + socket.id);
function sendAll(req,data){//向客户端广播
socket.broadcast.emit(req,data);
}
function recv(req,res){//接收客户端消息
socket.on(req,res);
}
recv("disconnect", () => {//某个用户断开连接的时告诉所有还在线的用户
console.log(socket.id+" disconnected");
sendAll(socket.id+'user disconnected');
});
recv("chat message",(msg) => {//转发聊天消息
console.log(socket.id + " say: " + msg);
sendAll("chat message",socket.id + " say: "+msg);
socket.emit("chat message",socket.id + " say: "+msg);
});
recv('new user greet', (data) => {//当有新用户加入告知所有在线用户。
console.log(socket.id + ' greet ' + data.msg);
sendAll('need connect', {sender: socket.id, msg : data.msg});
});
recv('ok we connect', (data) => {//在线用户回应新用户消息的转发
io.to(data.receiver).emit('ok we connect', {sender : data.sender});
});
recv( 'sdp', ( data ) => {//sdp 消息的转发
console.log('sdp: ' + data.sender + ' to:' + data.to);
socket.to( data.to ).emit( 'sdp', {
description: data.description,
sender: data.sender
} );
} );
recv( 'ice candidates', ( data ) => {//candidates 消息的转发
console.log('ice candidates: ' + data.sender + ' to:' + data.to);
socket.to( data.to ).emit( 'ice candidates', {
candidate: data.candidate,
sender: data.sender
} );
} );
});