Skip to content
This repository was archived by the owner on Feb 10, 2022. It is now read-only.

Commit 0221c12

Browse files
committed
Update Sample to 0.7.0
1 parent 1083658 commit 0221c12

File tree

9 files changed

+111
-32
lines changed

9 files changed

+111
-32
lines changed

envs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const TEST_APP_ID = 'YOUR_APP_ID';
1+
export const TEST_APP_ID = BUILD_APP_ID || 'YOUR_APP_ID';
22
export const USER_ID = undefined;
33
export const ACCESS_TOKEN = undefined;
44
export const IS_ACCESS_TOKEN_NEEDED = false;

lib/modules/periodicJob.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
export default class PeriodicJob {
4+
5+
constructor(jobFunc, interval = 1000) {
6+
this.jobFunc = jobFunc;
7+
this.interval = interval;
8+
this.lastResult = null;
9+
}
10+
11+
start() {
12+
this.stop();
13+
14+
this.timer = setInterval(() => {
15+
this.lastResult = this.jobFunc();
16+
}, this.interval);
17+
18+
return this;
19+
}
20+
21+
stop() {
22+
if (this.timer) {
23+
clearInterval(this.timer);
24+
this.timer = null;
25+
}
26+
return this;
27+
}
28+
}

lib/utils/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export function getCallOption() {
99
videoFps: 30,
1010
frontCamera: false
1111
}
12-
}
12+
}

lib/views/CallView.js

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { createDiv, createImg, createLabel } from "../utils/domUtil";
33
import { getCallOption } from "../utils/util";
44
import CallButtons from "./CallButtons";
55
import { classes } from "../css/styles.js";
6+
import PeriodicJob from "../modules/PeriodicJob";
67

78
export default class CallView extends BaseElement {
89
constructor({ call, state, args }) {
910
super({ id: 'call_view', className: `${classes['column']} ${classes['center']} ${classes['view']} ${classes['viewCall']}`, args });
1011

1112
this.call = call;
1213
this.state = state;
13-
this.timer = null;
1414
this.connected = false;
1515
this.addCallListeners(call);
1616
}
@@ -25,7 +25,7 @@ export default class CallView extends BaseElement {
2525
const peerProfile = createImg({ src: remoteUser.profileUrl, className: classes['remoteProfile'] });
2626
const peerName = createLabel({ id: 'peer_name', innerText: remoteUser.userId, className: `${classes['peerName']} ${classes['fontBig']}` });
2727

28-
const connectionInfo = createLabel({ id: 'conn_info_label', className: `${classes['connectionInfo']} ${classes['fontNormal']}` });
28+
this.connectionInfo = createLabel({ id: 'conn_info_label', className: `${classes['connectionInfo']} ${classes['fontNormal']}` });
2929

3030
const peerStateDiv = createDiv({ id: 'peer_state', className: `${classes['column']} ${classes['peerStateDiv']} ${classes['invisible']}` });
3131
const peerMuteIcon = createDiv({ id: 'peer_mute_icon', className: classes['peerMuteIcon'] });
@@ -37,41 +37,37 @@ export default class CallView extends BaseElement {
3737

3838
this.element.appendChild(peerProfile);
3939
this.element.appendChild(peerName);
40-
this.element.appendChild(connectionInfo);
40+
this.element.appendChild(this.connectionInfo);
4141
this.element.appendChild(peerStateDiv);
4242
buttons.appendToBaseElement(this);
4343

44-
let tick = 0;
45-
this.timer = setInterval(() => {
46-
const callingStr = 'Calling';
47-
if (this.call && this.call.endResult) {
48-
connectionInfo.innerText = this.call.endResult;
49-
clearInterval(this.timer);
50-
} else if (!this.connected && this.call.caller.userId === localUser.userId) {
51-
connectionInfo.innerText = callingStr + '.'.repeat((tick % 3) + 1);
52-
} else if (this.connected) {
53-
const durationInSec = Math.floor(this.call.getDuration() / 1000);
54-
const minutes = `0${Math.floor(durationInSec / 60)}`.slice(-2);
55-
const seconds = `0${durationInSec % 60}`.slice(-2);
56-
connectionInfo.innerText = `${minutes}:${seconds}`;
57-
}
58-
59-
tick += 1;
60-
}, 1000);
44+
if (this.call.caller.userId === localUser.userId) {
45+
this.drawCallingText();
46+
}
6147

6248
this.sendToChildren(this.state);
6349
}
6450

6551
addCallListeners(call) {
6652
call.onConnected = () => {
6753
this.connected = true;
54+
this.drawCurrentTime();
6855
this.sendToChildren('connected');
6956
};
7057

7158
call.onEnded = () => {
59+
this.drawEndResult();
7260
this.sendToChildren('ended');
7361
};
7462

63+
call.onReconnected = () => {
64+
this.drawCurrentTime();
65+
};
66+
67+
call.onReconnecting = () => {
68+
this.drawReconnectingText();
69+
};
70+
7571
call.onRemoteAudioSettingsChanged = (call) => {
7672
this.onRemoteMuted(call.isRemoteAudioEnabled);
7773
};
@@ -108,7 +104,7 @@ export default class CallView extends BaseElement {
108104
accept() {
109105
const callOption = getCallOption();
110106

111-
this.call.accept(callOption).catch((e) => {
107+
this.call.accept({ callOption: callOption }).catch((e) => {
112108
alert(e);
113109
});
114110
}
@@ -127,8 +123,51 @@ export default class CallView extends BaseElement {
127123

128124
close() {
129125
this.end();
130-
clearInterval(this.timer);
126+
this.removeConnectionInfoDrawer();
131127

132128
this.sendToParent('close', this.call);
133129
}
130+
131+
drawCallingText() {
132+
this.removeConnectionInfoDrawer();
133+
const rotatingTexts = ['Calling', 'Calling.', 'Calling..'];
134+
let frame = 0;
135+
this.connectionInfoDrawer = new PeriodicJob(() => {
136+
this.connectionInfo.innerText = rotatingTexts[frame];
137+
frame = (frame + 1) % rotatingTexts.length;
138+
}).start();
139+
}
140+
141+
drawReconnectingText() {
142+
this.removeConnectionInfoDrawer();
143+
const rotatingTexts = ['Reconnecting', 'Reconnecting.', 'Reconnecting..'];
144+
let frame = 0;
145+
this.connectionInfoDrawer = new PeriodicJob(() => {
146+
this.connectionInfo.innerText = rotatingTexts[frame];
147+
frame = (frame + 1) % rotatingTexts.length;
148+
}).start();
149+
}
150+
151+
drawCurrentTime() {
152+
this.removeConnectionInfoDrawer();
153+
this.connectionInfoDrawer = new PeriodicJob(() => {
154+
const durationInSec = Math.floor(this.call.getDuration() / 1000);
155+
const minutes = `0${Math.floor(durationInSec / 60)}`.slice(-2);
156+
const seconds = `0${durationInSec % 60}`.slice(-2);
157+
this.connectionInfo.innerText = `${minutes}:${seconds}`;
158+
}).start();
159+
}
160+
161+
drawEndResult() {
162+
this.removeConnectionInfoDrawer();
163+
this.connectionInfo.innerText = this.call.endResult;
164+
}
165+
166+
removeConnectionInfoDrawer() {
167+
if (this.connectionInfoDrawer) {
168+
this.connectionInfoDrawer.stop();
169+
this.connectionInfoDrawer = null;
170+
}
171+
this.connectionInfo.innerText = '';
172+
}
134173
}

lib/views/DialView.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ export default class DialView extends BaseElement {
5858

5959
try {
6060

61-
const call = SendBirdCall.dial(peerId, false, callOption, (call, error) => {
61+
const call = SendBirdCall.dial({
62+
userId: peerId,
63+
isVideoCall: false,
64+
callOption: callOption
65+
}, (call, error) => {
6266
if (error) {
6367
console.error('dial failed');
6468
return;

lib/views/WidgetDialView.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ export default class widgetDialView extends BaseElement {
7171
const callOption = getCallOption();
7272

7373
try {
74-
const call = SendBirdCall.dial(peerId, false, callOption, (call, error) => {
74+
const call = SendBirdCall.dial({
75+
userId: peerId,
76+
isVideoCall: false,
77+
callOption: callOption
78+
}, (call, error) => {
7579
if (error) {
7680
this.errorMsg.textContent = error.message;
7781
if (this.errorMsg.htmlFor) {

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"express": "^4.17.1",
3535
"jss": "^10.0.3",
3636
"jss-preset-default": "^10.0.3",
37-
"sendbird-calls": "^0.6.10",
37+
"sendbird-calls": "^0.7.0",
3838
"uuid": "^3.3.3"
3939
}
4040
}

webpack.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
22
const HtmlWebpackPlugin = require('html-webpack-plugin');
33
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
44
const path = require('path');
5+
const webpack = require("webpack");
56

6-
module.exports = {
7+
module.exports = env => ({
78
entry: {
89
main: ['./js/main.js'],
910
widget: ['./js/widget.js'],
@@ -70,6 +71,9 @@ module.exports = {
7071
plugins: [
7172
new CleanWebpackPlugin(),
7273
new MiniCssExtractPlugin(),
74+
new webpack.DefinePlugin({
75+
BUILD_APP_ID: (env && env.app_id) ? `"${env.app_id}"` : undefined,
76+
}),
7377
new HtmlWebpackPlugin({
7478
filename: 'index.html',
7579
template: 'views/index.html',
@@ -86,4 +90,4 @@ module.exports = {
8690
chunks: ['widget']
8791
})
8892
]
89-
};
93+
});

0 commit comments

Comments
 (0)