basic chat support
parent
bf45e41b49
commit
f881a74057
|
|
@ -15,9 +15,20 @@
|
||||||
<div class="btn-wrap">
|
<div class="btn-wrap">
|
||||||
<input type="button" id="start" onclick="start(true)" value="Connect"></input>
|
<input type="button" id="start" onclick="start(true)" value="Connect"></input>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="chatWrap" class="chat-wrap" style="visibility:hidden">
|
||||||
|
<div id="msgArea" class="msg-area">
|
||||||
|
MESSAG AREA<br>
|
||||||
|
</div>
|
||||||
|
<div class="chat-input">
|
||||||
|
<input type="text" id="chatInput">
|
||||||
|
<br><br>
|
||||||
|
<input type="button" id="send" onclick="sendMsg()" value="send"></input>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div id="statusWrap" class="status-wrap" style="visibility: hidden">
|
<div id="statusWrap" class="status-wrap" style="visibility: hidden">
|
||||||
Status: <span id="status">Page Loaded</span>
|
Status: <span id="status">Page Loaded</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
<div id="uuid">...</div>
|
<div id="uuid">...</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
@ -46,6 +57,15 @@
|
||||||
input[type="button"]:hover{
|
input[type="button"]:hover{
|
||||||
background: var(--accent2);
|
background: var(--accent2);
|
||||||
}
|
}
|
||||||
|
input[type="text"]{
|
||||||
|
background: rgb(45,45,45);
|
||||||
|
color:white;
|
||||||
|
font-size:1.1rem;
|
||||||
|
padding:0.5rem;
|
||||||
|
border: 2px solid white;
|
||||||
|
text-align:left;
|
||||||
|
width:calc(100% - 1rem);
|
||||||
|
}
|
||||||
.rnd{
|
.rnd{
|
||||||
border-radius:0.5rem;
|
border-radius:0.5rem;
|
||||||
}
|
}
|
||||||
|
|
@ -71,12 +91,22 @@
|
||||||
background: rgb(33,33,33);
|
background: rgb(33,33,33);
|
||||||
}
|
}
|
||||||
.status-wrap{
|
.status-wrap{
|
||||||
background: rgb(22,22,22);
|
background: rgb(22,55,22);
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
height:4.5rem;
|
height:4.5rem;
|
||||||
resize: vertical;
|
resize: vertical;
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
}
|
}
|
||||||
|
.chat-wrap{
|
||||||
|
background: rgb(33,33,33);
|
||||||
|
}
|
||||||
|
.msg-area{
|
||||||
|
background: rgb(65,65,65);
|
||||||
|
padding:1rem;
|
||||||
|
}
|
||||||
|
.chat-input{
|
||||||
|
text-align:right;
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ let _s = {
|
||||||
localStream: undefined,
|
localStream: undefined,
|
||||||
remoteVideo: undefined,
|
remoteVideo: undefined,
|
||||||
peerConnection: undefined,
|
peerConnection: undefined,
|
||||||
|
dataChannel: undefined,
|
||||||
uuid: undefined,
|
uuid: undefined,
|
||||||
serverConnection: undefined,
|
serverConnection: undefined,
|
||||||
statusLog: ''
|
statusLog: ''
|
||||||
|
|
@ -16,7 +17,7 @@ const peerConnectionConfig = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function pageReady() {
|
function pageReady() {
|
||||||
showStatus(true);
|
showStatus(false);
|
||||||
setStatus('Page Ready');
|
setStatus('Page Ready');
|
||||||
_s.uuid = createUUID();
|
_s.uuid = createUUID();
|
||||||
document.getElementById('uuid').innerHTML = 'UUID: '+_s.uuid;
|
document.getElementById('uuid').innerHTML = 'UUID: '+_s.uuid;
|
||||||
|
|
@ -32,6 +33,14 @@ function pageReady() {
|
||||||
audio: true,
|
audio: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
document.getElementById('chatInput').addEventListener("keypress", function(event) {
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
event.preventDefault();
|
||||||
|
document.getElementById("send").click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
if(navigator.mediaDevices.getUserMedia) {
|
if(navigator.mediaDevices.getUserMedia) {
|
||||||
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -40,7 +49,11 @@ function pageReady() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function showStatus(show=true){
|
function showStatus(show=true){
|
||||||
document.getElementById('statusWrap').style.visibility = show ? "visible" : "hidden";
|
document.getElementById('statusWrap').style.visibility = show ? 'visible' : 'hidden';
|
||||||
|
}
|
||||||
|
|
||||||
|
function showChat(show=true){
|
||||||
|
document.getElementById('chatWrap').style.visibility = show ? 'visible' : 'hidden';
|
||||||
}
|
}
|
||||||
|
|
||||||
function setStatus(msg){
|
function setStatus(msg){
|
||||||
|
|
@ -48,11 +61,64 @@ function setStatus(msg){
|
||||||
document.getElementById('status').innerHTML = _s.statusLog;
|
document.getElementById('status').innerHTML = _s.statusLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendMsg(){
|
||||||
|
const content = _s.uuid + ': ' + document.getElementById('chatInput').value;
|
||||||
|
_s.dataChannel.send(content);
|
||||||
|
// write local
|
||||||
|
writeMsg(content.replace(_s.uuid, 'You'));
|
||||||
|
document.getElementById('chatInput').value = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeMsg(msg){
|
||||||
|
//alert(msg);
|
||||||
|
document.getElementById('msgArea').innerHTML += msg + '<br>';
|
||||||
|
}
|
||||||
|
|
||||||
function getUserMediaSuccess(stream) {
|
function getUserMediaSuccess(stream) {
|
||||||
_s.localStream = stream;
|
_s.localStream = stream;
|
||||||
_s.localVideo.srcObject = stream;
|
_s.localVideo.srcObject = stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupDataChannel(){
|
||||||
|
_s.peerConnection.ondatachannel = (event) => {
|
||||||
|
event.channel.onmessage = (e) =>{
|
||||||
|
writeMsg(e.data);
|
||||||
|
};
|
||||||
|
event.channel.onerror = (e) =>{
|
||||||
|
alert(e);
|
||||||
|
}
|
||||||
|
event.channel.onopen = (e) =>{
|
||||||
|
//alert('OPEN');
|
||||||
|
};
|
||||||
|
event.channel.onclose = (e) =>{
|
||||||
|
//alert('CLOSE');
|
||||||
|
sendMsg('>> Peer DisconnectedX!');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
_s.dataChannel = _s.peerConnection.createDataChannel('dataChannel');
|
||||||
|
_s.dataChannel.onopen = () => {
|
||||||
|
// The data channel is now open
|
||||||
|
// You can now send data
|
||||||
|
// alert('DC OPEN');
|
||||||
|
_s.dataChannel.send(_s.uuid + ': CONNECTED!');
|
||||||
|
showChat(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
_s.dataChannel.onmessage = function(event){
|
||||||
|
console.log(event);
|
||||||
|
alert('GOT MSG');
|
||||||
|
}
|
||||||
|
|
||||||
|
_s.dataChannel.onclose = function(event){
|
||||||
|
writeMsg('>> Peer Disconnected!');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function start(isCaller) {
|
function start(isCaller) {
|
||||||
setStatus('Starting');
|
setStatus('Starting');
|
||||||
_s.peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
_s.peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||||
|
|
@ -63,6 +129,8 @@ function start(isCaller) {
|
||||||
if(isCaller) {
|
if(isCaller) {
|
||||||
_s.peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
_s.peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
||||||
}
|
}
|
||||||
|
setupDataChannel();
|
||||||
|
writeMsg('>> Connected to peer!');
|
||||||
}
|
}
|
||||||
|
|
||||||
function gotMessageFromServer(message) {
|
function gotMessageFromServer(message) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Setting up a data connection in JS using WebRTC is relatively straightforward. First, you need to create a RTCPeerConnection object. This object will be used to establish the connection between two peers.
|
||||||
|
|
||||||
|
// Create an RTCPeerConnection object
|
||||||
|
let pc = new RTCPeerConnection();
|
||||||
|
|
||||||
|
// Create a data channel
|
||||||
|
let dataChannel = pc.createDataChannel('dataChannel');
|
||||||
|
|
||||||
|
// Set up the ICE candidate handler
|
||||||
|
pc.onicecandidate = (e) => {
|
||||||
|
if (e.candidate) {
|
||||||
|
// Send the candidate to the remote peer
|
||||||
|
sendIceCandidate(e.candidate);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set up the data channel handler
|
||||||
|
dataChannel.onopen = () => {
|
||||||
|
// The data channel is now open
|
||||||
|
// You can now send data
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create an offer
|
||||||
|
pc.createOffer().then((offer) => {
|
||||||
|
// Set the local description
|
||||||
|
pc.setLocalDescription(offer);
|
||||||
|
// Send the offer to the remote peer
|
||||||
|
sendOffer(offer);
|
||||||
|
});
|
||||||
|
|
||||||
|
// When the remote peer sends an answer
|
||||||
|
pc.setRemoteDescription(answer);
|
||||||
|
|
||||||
|
// When the remote peer sends an ICE candidate
|
||||||
|
pc.addIceCandidate(candidate);
|
||||||
|
|
||||||
|
// Now the connection is established and you can send data using the dataChannel object
|
||||||
|
|
||||||
Loading…
Reference in New Issue