Skip to content

Commit 32facd2

Browse files
authored
Merge pull request #44 from oslabs-beta/doc
updated documentation and modularized code
2 parents ee7f98b + 90cb1dd commit 32facd2

File tree

12 files changed

+230
-92
lines changed

12 files changed

+230
-92
lines changed

dist/src/components/Socket.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/// <reference types="react" />
2+
/**
3+
* @file Socket.tsx is the component that initalizes and loads the client's socket connection with event listeners.
4+
*/
25
declare type SocketType = {
36
ws: WebSocket;
47
getUsers: (parsedData: {
@@ -18,9 +21,10 @@ declare type SocketType = {
1821
};
1922
/**
2023
* @desc Using the initial WebSocket connection, this functional component provides the event listeners for each client's socket connection to allow bilateral communication.
21-
* @param {Object} props
22-
* @param {string} props.ws - the socket that will initiate the connection with the WebSocket server
23-
* @param props.getUsers - the functions that are executed upon on each switch case event.
24+
* @param {string} props.ws - the ws or wss socket url that will initiate the connection with the WebSocket server
25+
* @param {function} props.getUser - When data (the list of connected users) is received from the WebSocketServer/backend, getUser
26+
* function is invoked and it updates the userList state so that the list of currently connected users
27+
* can be displayed on the frontend.
2428
* @param props.handleReceiveCall
2529
* @param props.handleAnswer
2630
* @param props.handleNewIceCandidate

dist/src/components/Socket.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const actions_1 = __importDefault(require("../constants/actions"));
88
const { LOGIN, ICECANDIDATE, OFFER, ANSWER, LEAVE } = actions_1.default;
99
/**
1010
* @desc Using the initial WebSocket connection, this functional component provides the event listeners for each client's socket connection to allow bilateral communication.
11-
* @param {Object} props
12-
* @param {string} props.ws - the socket that will initiate the connection with the WebSocket server
13-
* @param props.getUsers - the functions that are executed upon on each switch case event.
11+
* @param {string} props.ws - the ws or wss socket url that will initiate the connection with the WebSocket server
12+
* @param {function} props.getUser - When data (the list of connected users) is received from the WebSocketServer/backend, getUser
13+
* function is invoked and it updates the userList state so that the list of currently connected users
14+
* can be displayed on the frontend.
1415
* @param props.handleReceiveCall
1516
* @param props.handleAnswer
1617
* @param props.handleNewIceCandidate
@@ -53,7 +54,7 @@ const Socket = ({ ws, getUsers, handleReceiveCall, handleAnswer, handleNewIceCan
5354
}
5455
});
5556
})();
56-
// an empty jsx element is rendered because this component is only meant to initalize and load the client's socket connection with event listeners
57+
// should return an empty JSX fragment
5758
return (react_1.default.createElement(react_1.default.Fragment, null));
5859
};
5960
exports.default = Socket;

dist/src/components/VideoCall.d.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
/// <reference types="react" />
22
/**
3-
* @desc Wrapper component containing the logic necessary for P2P connections using WebRTC APIs (RTCPeerConnect API + MediaSession API) and WebSockets.
3+
* @desc Wrapper component containing the logic necessary for peer connections using WebRTC APIs (RTCPeerConnect API + MediaSession API) and WebSockets.
44
*
5-
* Any client can call another client and thus not all functions are invoked for every user.
5+
* ws, localVideo, remoteVideo, peerRef, localStream, otherUser, senders are all mutable ref objects that are created using the useRef hook. The useRef hook allows you to persist values between renders and it is used to store a mutable value that does NOT cause a re-render when updated.
66
*
7+
* The WebSocket connection (ws.current) is established using the useEffect hook and once the component mounts, the Socket component is rendered. The Socket component adds event listeners that handle the offer-answer model and the exchange of SDP objects between peers and the socket.
8+
*
9+
* The WebSocket message event will filter through various events to determine the payloads that will be sent to other serverside socket connection via WebSocket.
10+
*
11+
* @type {object} ws is the mutable ref object that contains the WebSocket object (ws.current). The ws.current WebSocket object will be created using the useEffect hook and it will establish the WebSocket connection to the server.
712
* ws.current.send enqueues the specified messages that need to be transmitted to the server over the WebSocket connection and this WebSocket connection is connected to the server by using RTConnect's importable SignalingChannel module.
13+
* @type {state} username - username state stores the name the client enters. All users (see getUsers) will be able to see an updated list of all other users whenever a new user logs in or leaves.
14+
* @type {state} users - users state is the list of connected users that is rendered on the frontend.
815
*
9-
* @param {object} props
10-
* @param {string} props.URL ws or wss link
16+
* @param {Object} props
17+
* @param {String} props.URL - ws or wss link
1118
* @param {object} props.mediaOptions video embed attributes
12-
* @returns A component that renders two VideoComponents
19+
* @returns A component that renders two VideoComponents,
1320
*/
1421
declare const VideoCall: ({ URL, mediaOptions }: {
1522
URL: string;

dist/src/components/VideoCall.js

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,70 @@ const mediaStreamConstraints_1 = __importDefault(require("../constants/mediaStre
4444
const rtcConfiguration_1 = __importDefault(require("../constants/rtcConfiguration"));
4545
const { LOGIN, ICECANDIDATE, OFFER, ANSWER, LEAVE } = actions_1.default;
4646
/**
47-
* @desc Wrapper component containing the logic necessary for P2P connections using WebRTC APIs (RTCPeerConnect API + MediaSession API) and WebSockets.
47+
* @desc Wrapper component containing the logic necessary for peer connections using WebRTC APIs (RTCPeerConnect API + MediaSession API) and WebSockets.
4848
*
49-
* Any client can call another client and thus not all functions are invoked for every user.
49+
* ws, localVideo, remoteVideo, peerRef, localStream, otherUser, senders are all mutable ref objects that are created using the useRef hook. The useRef hook allows you to persist values between renders and it is used to store a mutable value that does NOT cause a re-render when updated.
5050
*
51+
* The WebSocket connection (ws.current) is established using the useEffect hook and once the component mounts, the Socket component is rendered. The Socket component adds event listeners that handle the offer-answer model and the exchange of SDP objects between peers and the socket.
52+
*
53+
* The WebSocket message event will filter through various events to determine the payloads that will be sent to other serverside socket connection via WebSocket.
54+
*
55+
* @type {object} ws is the mutable ref object that contains the WebSocket object (ws.current). The ws.current WebSocket object will be created using the useEffect hook and it will establish the WebSocket connection to the server.
5156
* ws.current.send enqueues the specified messages that need to be transmitted to the server over the WebSocket connection and this WebSocket connection is connected to the server by using RTConnect's importable SignalingChannel module.
57+
* @type {state} username - username state stores the name the client enters. All users (see getUsers) will be able to see an updated list of all other users whenever a new user logs in or leaves.
58+
* @type {state} users - users state is the list of connected users that is rendered on the frontend.
5259
*
53-
* @param {object} props
54-
* @param {string} props.URL ws or wss link
60+
* @param {Object} props
61+
* @param {String} props.URL - ws or wss link
5562
* @param {object} props.mediaOptions video embed attributes
56-
* @returns A component that renders two VideoComponents
63+
* @returns A component that renders two VideoComponents,
5764
*/
5865
const VideoCall = ({ URL, mediaOptions }) => {
59-
// The username state stores the name the client enters. All users (see getUsers) will be able to see an updated list of all other users whenever a new user logs in or leaves.
6066
const [username, setUsername] = (0, react_1.useState)('');
6167
const [users, setUsers] = (0, react_1.useState)();
62-
// The useRef hook allows our variables to be stored as Immutable Objects and thus they do not force page re-renders when their values are changed.
63-
// The WebSocket connection is established using the useEffect hook - once the component mounts we then render the Socket component, which adds event listeners that can handle the offer-answer model and SDP objects being exchanged between peers to the socket.
68+
/**
69+
* @type {mutable ref WebSocket object} ws contains the WebSocket object (ws.current). It cannot be null or undefined.
70+
*
71+
* The ws.current WebSocket object will be created using the useEffect hook and it will establish the WebSocket connection to the server.
72+
*/
6473
const ws = (0, react_1.useRef)(null);
74+
/**
75+
* @type {mutable ref object} localVideo - video stream of the local user. It will not be null or undefined.
76+
*/
6577
const localVideo = (0, react_1.useRef)(null);
78+
/**
79+
* @type {mutable ref object} remoteVideo - video stream of the remote user. It cannot be null or undefined.
80+
*/
6681
const remoteVideo = (0, react_1.useRef)(null);
82+
/**
83+
* @type {mutable ref object} peerRef - It cannot be null or undefined.
84+
*/
6785
const peerRef = (0, react_1.useRef)(null);
86+
/**
87+
* @type {mutable ref string} otherUser -
88+
*/
6889
const otherUser = (0, react_1.useRef)();
90+
/**
91+
* @type {mutable ref object} localStream - It cannot be null or undefined.
92+
*/
6993
const localStream = (0, react_1.useRef)(null);
94+
/**
95+
* @type {mutable ref array} senders -
96+
*/
7097
const senders = (0, react_1.useRef)([]);
98+
/**
99+
* @type {string} userField - the username that is entered in the input field when the Submit Username
100+
* button is clicked.
101+
*/
71102
let userField = '';
72103
let receiver = '';
73-
// a new one-time WebSocket connection is made on component mount and a permissions request for the client's video and audio is made
104+
/**
105+
* @desc
106+
* A WebSocket connection is made on component mount and the function openUserMedia is invoked, which
107+
* makes a permissions request for the client's video and audio is made
108+
* @prop {object} ws.current
109+
110+
*/
74111
(0, react_1.useEffect)(() => {
75112
ws.current = new WebSocket(URL);
76113
openUserMedia();
@@ -97,8 +134,9 @@ const VideoCall = ({ URL, mediaOptions }) => {
97134
*/
98135
/**
99136
* @desc An event triggered on a button click.
100-
* Once the client enters and submits a name in the username field, this name is set stored in the WebSocketServer along with the socket
101-
* that sent the name to later send messages to the right client using this socket.
137+
* Once the client enters and submits a name in the username field, this name is set stored in the
138+
* WebSocketServer along with the socket that sent the name to later send messages to the right client
139+
* using this socket.
102140
*/
103141
const handleUsername = () => {
104142
const loginPayload = {
@@ -124,18 +162,24 @@ const VideoCall = ({ URL, mediaOptions }) => {
124162
}
125163
};
126164
/**
127-
* @desc When data is received from the WebSocketServer, this function is invoked.
128-
* Based off of the users connected, each user is displayed in a div.
129-
* @param {Array<string>} parsedData
130-
* @returns Re-renders the page with the new User List
165+
* @function getUser
166+
* @desc When data (the list of connected users) is received from the WebSocketServer/backend, getUser
167+
* function is invoked and it updates the userList state so that the list of currently connected users
168+
* can be displayed on the frontend.
169+
* @param {Array<string>} parsedData - data (the array of usernames that are connected) that is
170+
* returned from backend/WebSocketServer.
171+
* @returns Re-renders the page with the new User List
131172
*/
132173
const getUsers = (parsedData) => {
133174
const userList = parsedData.payload.map((name, idx) => (react_1.default.createElement("div", { key: idx }, name)));
134175
setUsers(userList);
135176
};
136177
/**
137-
* @desc Asks for the client's permissions to open their webcam and microphone.
138-
*/
178+
* @async
179+
* @function openUserMedia
180+
* @param
181+
* @desc Asks for the client's permissions to open their webcam and microphone.
182+
*/
139183
const openUserMedia = () => __awaiter(void 0, void 0, void 0, function* () {
140184
try {
141185
if (localVideo.current) {
@@ -189,8 +233,9 @@ const VideoCall = ({ URL, mediaOptions }) => {
189233
return peer;
190234
};
191235
/**
192-
* @desc invokes WebRTC's built-in createOffer() function to create an SDP offer, which is an RTCSessionDescription object. This offer is then set as the local description using WebRTC's built-in setLocalDescription() function. Finally, the offer, sender and receiver is sent via ws.current.send to the Signaling Channel in the backend
193-
* @param {string} userID
236+
* @function handleNegotiationNeededEvent
237+
* @desc invokes WebRTC's built-in createOffer() function to create an SDP offer, which is an RTCSessionDescription object. This offer is then set as the local description using WebRTC's built-in setLocalDescription() function. Finally, the offer, sender and receiver is sent via ws.current.send to the Signaling Channel in the backend
238+
* @param {string} userID
194239
*/
195240
function handleNegotiationNeededEvent(userID) {
196241
var _a;
@@ -331,8 +376,8 @@ const VideoCall = ({ URL, mediaOptions }) => {
331376
*/
332377
return (react_1.default.createElement(react_1.default.Fragment, null,
333378
ws.current ?
334-
react_1.default.createElement(Socket_1.default, { ws: ws.current, getUsers: getUsers, handleReceiveCall: handleReceiveCall, handleAnswer: handleAnswer, handleNewIceCandidate: handleNewIceCandidate, endCall: endCall })
335-
: '',
379+
react_1.default.createElement(Socket_1.default, { ws: ws.current, getUsers: getUsers, handleReceiveCall: handleReceiveCall, handleAnswer: handleAnswer, handleNewIceCandidate: handleNewIceCandidate, endCall: endCall }) :
380+
'',
336381
react_1.default.createElement("div", { className: '', style: { display: 'flex', justifyContent: 'space-around', flexDirection: 'column', padding: '10px', marginTop: '10%' } },
337382
username === '' ?
338383
react_1.default.createElement(react_1.default.Fragment, null,

dist/src/constants/actions.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare type ActionType = {
99
JOIN_ROOM: string;
1010
};
1111
/**
12+
* @constant {object} actions
1213
* @desc actions used by the SignalingChannel and Socket component to filter and identify the data being passed through the WebSocket message event.
1314
*/
1415
declare const actions: ActionType;

dist/src/constants/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
/**
4+
* @constant {object} actions
45
* @desc actions used by the SignalingChannel and Socket component to filter and identify the data being passed through the WebSocket message event.
56
*/
67
const actions = {

dist/src/constants/rtcConfiguration.d.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
/**
22
* @type {Object} configuration - The RTCConfiguration object is used to provide configuration options for how an RTCPeerConnection should be created.
3+
*
34
* @property {array} iceServers - An array of RTCIceServer objects, each describing one server which
4-
* may be used by the ICE agent. These servers are typically STUN and/or TURN servers. If this isn't
5-
* specified, the connection attempt will be made with no STUN or TURN server available, which limits
6-
* the connection to local peers. STUN servers are used to identify users so that peers can connect to
7-
* each other directly without the use of a middleman server (which would create latency).
5+
* may be used by the ICE agent. These servers are typically STUN and/or TURN servers. If this
6+
* property isn't specified, the connection attempt will be made with no STUN or TURN server
7+
* available, which limits the connection to local peers.
8+
*
9+
* STUN servers are used to identify users so that peers can connect to each other directly without
10+
* the use of a middleman server (which would create latency).
11+
*
812
* Another RTCIceServer is 'stun:stun2.l.google.com:19302'.
9-
* @property {number} iceCandidatePoolSize - this specifies the size of the prefetched ICE candidate
10-
* pool. The default value is 0 (meaning no candidate prefetching will occur). In some cases,
11-
* connections can be established more quickly by allowing the ICE agent to start fetching ICE
12-
* candidates before the connection is started, so that ICE candidates are already available to be
13-
* inspected by the time RTCPeerConnection.setLocalDescription() is called. Changing the size of the
14-
* ICE candidate pool may trigger the beginning of ICE trickling.
13+
*
14+
* @property {number} iceCandidatePoolSize - specifies the size of the prefetched ICE candidate
15+
* pool. The default value is 0 (meaning no candidate prefetching will occur).
16+
*
17+
* In some cases, connections can be established more quickly by allowing the ICE agent to start
18+
* fetching ICE candidates before the connection is started, so that ICE candidates are already
19+
* available to be inspected by the time RTCPeerConnection.setLocalDescription() is called.
20+
*
21+
* Changing the size of the ICE candidate pool may trigger the beginning of ICE trickling.
1522
*
1623
*/
1724
declare const configuration: RTCConfiguration;

dist/src/constants/rtcConfiguration.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
/**
44
* @type {Object} configuration - The RTCConfiguration object is used to provide configuration options for how an RTCPeerConnection should be created.
5+
*
56
* @property {array} iceServers - An array of RTCIceServer objects, each describing one server which
6-
* may be used by the ICE agent. These servers are typically STUN and/or TURN servers. If this isn't
7-
* specified, the connection attempt will be made with no STUN or TURN server available, which limits
8-
* the connection to local peers. STUN servers are used to identify users so that peers can connect to
9-
* each other directly without the use of a middleman server (which would create latency).
7+
* may be used by the ICE agent. These servers are typically STUN and/or TURN servers. If this
8+
* property isn't specified, the connection attempt will be made with no STUN or TURN server
9+
* available, which limits the connection to local peers.
10+
*
11+
* STUN servers are used to identify users so that peers can connect to each other directly without
12+
* the use of a middleman server (which would create latency).
13+
*
1014
* Another RTCIceServer is 'stun:stun2.l.google.com:19302'.
11-
* @property {number} iceCandidatePoolSize - this specifies the size of the prefetched ICE candidate
12-
* pool. The default value is 0 (meaning no candidate prefetching will occur). In some cases,
13-
* connections can be established more quickly by allowing the ICE agent to start fetching ICE
14-
* candidates before the connection is started, so that ICE candidates are already available to be
15-
* inspected by the time RTCPeerConnection.setLocalDescription() is called. Changing the size of the
16-
* ICE candidate pool may trigger the beginning of ICE trickling.
15+
*
16+
* @property {number} iceCandidatePoolSize - specifies the size of the prefetched ICE candidate
17+
* pool. The default value is 0 (meaning no candidate prefetching will occur).
18+
*
19+
* In some cases, connections can be established more quickly by allowing the ICE agent to start
20+
* fetching ICE candidates before the connection is started, so that ICE candidates are already
21+
* available to be inspected by the time RTCPeerConnection.setLocalDescription() is called.
22+
*
23+
* Changing the size of the ICE candidate pool may trigger the beginning of ICE trickling.
1724
*
1825
*/
1926
const configuration = {

0 commit comments

Comments
 (0)