- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.html
More file actions
Latest commit
135 lines (108 loc) · 4.4 KB
/
Copy pathclient2.html
File metadata and controls
135 lines (108 loc) · 4.4 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<title>Test</title>
</head>
<body>
<videoautoplay></video>
<script>
// const from = 1;
// const to = 2;
constfrom=2;
constto=1;
constremoteView=document.querySelector('video');
classSignalingChannel{
constructor(url){
this.url=url;
this.socket=newWebSocket(url);
}
send(data){
varsocket=this.socket;
socket.send(JSON.stringify(data))
// socket.addEventListener('open', function (msg) {
// console.log("sending")
// socket.send(msg);
// });
}
addListener(handler){
this.socket.addEventListener('message',handler);
}
};
constsignaling=newSignalingChannel(`ws://192.168.1.205:8080/?from=${from}&to=${to}`);
constconstraints={audio: false,video: true};
constconfiguration={iceServers: [{urls: 'stun:stun.iptel.org'}]};
constpc=newRTCPeerConnection(configuration);
pc.onicecandidate=({ candidate })=>{
console.log(`ice candidate ${JSON.stringify(candidate)}`)
letmsg={"id": to,"body": JSON.stringify({"type": "candidate","body": candidate})}
signaling.send(msg)
};
pc.onnegotiationneeded=async()=>{
console.log('Negotiation is needed')
try{
awaitpc.setLocalDescription(awaitpc.createOffer());
// send the offer to the other peer
letmsg={"id": to,"body": JSON.stringify({"type": "desc","body": pc.localDescription})}
signaling.send(msg);
// signaling.send({ desc: pc.localDescription });
}catch(err){
console.error(err);
}
};
pc.ontrack=(event)=>{
// don't set srcObject again if it is already set.
if(remoteView.srcObject)return;
remoteView.srcObject=event.streams[0];
console.log("New track received")
};
asyncfunctionstart(){
try{
// get local stream, show it in self-view and add it to be sent
conststream=
awaitnavigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track)=>
pc.addTrack(track,stream));
// selfView.srcObject = stream;
}catch(err){
console.error(err);
}
}
constlistener=async(data)=>{
try{
console.log(`Signaling message received ${JSON.stringify(data)}`)
if(data.body!=null){
varobj=JSON.parse(data.body)
if(obj.type=="desc"){
desc=obj;
// if we get an offer, we need to reply with an answer
if(desc.type==='offer'){
awaitpc.setRemoteDescription(desc);
conststream=
awaitnavigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track)=>
pc.addTrack(track,stream));
awaitpc.setLocalDescription(awaitpc.createAnswer());
letmsg={"id": to,"body": JSON.stringify({"type": "desc","body": pc.localDescription})}
signaling.send(msg);
}elseif(desc.type==='answer'){
awaitpc.setRemoteDescription(desc);
}else{
console.log('Unsupported SDP type.');
}
}elseif(obj.type=="candidate"){
awaitpc.addIceCandidate(obj);
}
}
}catch(err){
console.error(err);
}
};
signaling.addListener(data=>{
console.log("adding listener again")
listener(data).then(res=>{
console.log("Finished")
});
});
start().then(result=>console.log("started"));
</script>
</body>
</html>