-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataChannel.html
42 lines (39 loc) · 1.28 KB
/
dataChannel.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DataChannel</title>
</head>
<body>
<div class="wrapper">
<div class="dialog"></div>
<div class="inputContent">
<textarea name="content" id="inputMess" cols="30" rows="10" placeholder="Input the message">
</textarea>
</div>
</div>
<script>
const PeerConnection=(window.PeerConnection)||
window.webkitPeerConnection00||
window.webkitRTCPeerConnection||
window.mozRTCPeerConnection;
const server = {
iceServers: [{
urls: 'stun:stun.l.google.com:19302'
}]
};
const pc=new PeerConnection(server,{optional: [{RtpDataChannels: true}]});
pc.ondatachannel=function(evt){
let receiveChannel=evt.channel;
receiveChannel.onmessage= function(evt){
console.log(evt.data);
}
}
const sendChannel=pc.createDataChannel("sendChannel",{reliable:false});
document.querySelector("submit").addEventListener("click",function(evt){
let sendData=document.querySelector("#inputMess").value;
sendChannel.send(sendData);
},false)
</script>
</body>
</html>