-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
120 lines (101 loc) · 4.17 KB
/
functions.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
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
// Establish a WebSocket connection with the server
const socket = new WebSocket('ws://' + window.location.host + '/websocket');
let webRTCConnection;
// Allow users to send messages by pressing enter instead of clicking the Send button
document.addEventListener("keypress", function (event) {
if (event.code === "Enter") {
sendMessage();
}
});
// Read the comment the user is sending to chat and send it to the server over the WebSocket as a JSON string
function sendMessage() {
const chatBox = document.getElementById("chat-comment");
const comment = chatBox.value;
const token = document.getElementById("chatToken").value;
chatBox.value = "";
chatBox.focus();
if (comment !== "") {
socket.send(JSON.stringify({'messageType': 'chatMessage', 'comment': comment, 'token': token}));
}
}
// Renders a new chat message to the page
function addMessage(chatMessage) {
let chat = document.getElementById('chat');
chat.innerHTML += "<b>" + chatMessage['username'] + "</b>: " + chatMessage["comment"] + "<br/>";
}
// called when the page loads to get the chat_history
function get_chat_history() {
const request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
const messages = JSON.parse(this.response);
for (const message of messages) {
addMessage(message);
}
}
};
request.open("GET", "/chat-history");
request.send();
}
// Called whenever data is received from the server over the WebSocket connection
socket.onmessage = function (ws_message) {
const message = JSON.parse(ws_message.data);
const messageType = message.messageType
switch (messageType) {
case 'chatMessage':
addMessage(message);
break;
case 'webRTC-offer':
webRTCConnection.setRemoteDescription(new RTCSessionDescription(message.offer));
webRTCConnection.createAnswer().then(answer => {
webRTCConnection.setLocalDescription(answer);
socket.send(JSON.stringify({'messageType': 'webRTC-answer', 'answer': answer}));
});
break;
case 'webRTC-answer':
webRTCConnection.setRemoteDescription(new RTCSessionDescription(message.answer));
break;
case 'webRTC-candidate':
webRTCConnection.addIceCandidate(new RTCIceCandidate(message.candidate));
break;
default:
console.log("received an invalid WS messageType");
}
}
function startVideo() {
const constraints = {video: true, audio: true};
navigator.mediaDevices.getUserMedia(constraints).then((myStream) => {
const elem = document.getElementById("myVideo");
elem.srcObject = myStream;
// Use Google's public STUN server
const iceConfig = {
'iceServers': [{'url': 'stun:stun2.1.google.com:19302'}]
};
// create a WebRTC connection object
webRTCConnection = new RTCPeerConnection(iceConfig);
// add your local stream to the connection
webRTCConnection.addStream(myStream);
// when a remote stream is added, display it on the page
webRTCConnection.onaddstream = function (data) {
const remoteVideo = document.getElementById('otherVideo');
remoteVideo.srcObject = data.stream;
};
// called when an ice candidate needs to be sent to the peer
webRTCConnection.onicecandidate = function (data) {
socket.send(JSON.stringify({'messageType': 'webRTC-candidate', 'candidate': data.candidate}));
};
})
}
function connectWebRTC() {
// create and send an offer
webRTCConnection.createOffer().then(webRTCOffer => {
socket.send(JSON.stringify({'messageType': 'webRTC-offer', 'offer': webRTCOffer}));
webRTCConnection.setLocalDescription(webRTCOffer);
});
}
function welcome() {
document.getElementById("paragraph").innerHTML += "<br/>This text was added by JavaScript 😀"
get_chat_history()
// use this line to start your video without having to click a button. Helpful for debugging
// startVideo();
}