-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (174 loc) · 5.88 KB
/
index.js
File metadata and controls
196 lines (174 loc) · 5.88 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var loginButton = document.querySelector('button#loginButton');
var logoutButton = document.querySelector('button#logoutButton');
var callButton = document.querySelector('button#callButton');
var hangButton = document.querySelector('button#hangButton');
var dialedButton = document.querySelector('button#dialedButton')
dialedButton.disabled = true;
loginButton.onclick = login
logoutButton.onclick = logout
callButton.onclick = call
hangButton.onclick = hangup
dialedButton.onclick = dialed
var userID = "user0";
var userSig = window.genTestUserSig(userID).userSig;
var roomID = "10086";
var callID = "user1";
let Trtc;
let startT;
// 初始化 SDK 实例
const tim = TIM.create({
SDKAppID: window.genTestUserSig('').SDKAppID
})
window.setLogLevel = tim.setLogLevel
tim.on(TIM.EVENT.MESSAGE_RECEIVED, onReceiveMessage)
tim.on(TIM.EVENT.SDK_READY, onReadyStateUpdate)
// 无日志级别
// tim.setLogLevel(4)
function onReadyStateUpdate() {
console.log("ready update...")
};
function onReceiveMessage({ data: messageList }) {
console.log("recieve...");
document.querySelector('button#dialedButton').disabled = false;
handleVideoMessage(messageList);
};
function handleVideoMessage(messageList) {
const videoMessageList = messageList.filter(
message => message.type === this.TIM.TYPES.MSG_CUSTOM && this.isJsonStr(message.payload.data)
)
if (videoMessageList.length === 0) return
const videoPayload = JSON.parse(videoMessageList[0].payload.data)
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_DIALING) {
if (this.isBusy) {
this.$bus.$emit('busy', videoPayload, videoMessageList[0])
return
}
this.$store.commit('GENERATE_VIDEO_ROOM', videoPayload.room_id)
this.selectConversation(videoMessageList[0].conversationID) // 切换当前会话页
if (videoMessageList[0].from !== this.userID) {
this.$bus.$emit('isCalled')
}
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_SPONSOR_CANCEL) {
this.$bus.$emit('missCall')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_REJECT) {
this.$bus.$emit('isRefused')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_SPONSOR_TIMEOUT) {
this.$bus.$emit('missCall')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_ACCEPTED) {
this.$bus.$emit('isAccept')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_HANGUP) {
this.$bus.$emit('isHungUp')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_LINE_BUSY) {
this.$bus.$emit('isRefused')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_ERROR) {
this.$bus.$emit('isRefused')
}
};
function login() {
console.log("loginbutton")
let promise = tim.login({userID: userID, userSig: userSig});
promise.then(function(imResponse) {
console.log(imResponse.data); // 登录成功
if (imResponse.data.repeatLogin === true) {
// 标识账号已登录,本次登录操作为重复登录。v2.5.1 起支持
console.log(imResponse.data.errorInfo);
}
}).catch(function(imError) {
console.warn('login error:', imError); // 登录失败的相关信息
});
tim.on(TIM.EVENT.MESSAGE_RECEIVED, onReceiveMessage)
tim.on(TIM.EVENT.SDK_READY, onReadyStateUpdate)
console.log("login success")
};
function logout() {
let promise = tim.logout();
promise.then(function(imResponse) {
console.log(imResponse.data); // 登出成功
}).catch(function(imError) {
console.warn('logout error:', imError);
});
};
async function initTrtc(options) { // 初始化 trtc 进入房间
Trtc = new RtcClient(options)
await Trtc.createLocalStream({ audio: true, video: true }).then(() => { // 在进房之前,判断设备
Trtc.join()
}).catch(() => {
alert(
'请确认已连接摄像头和麦克风并授予其访问权限!'
)
})
};
function sendVideoMessage(action, duration = 0) {
const options = {
room_id: roomID,
call_id: userID,
action,
version: VERSION,
invited_list: [],
duration
}
const message = tim.createCustomMessage({
to: callID,
conversationType: TIM.TYPES.CONV_C2C,
payload: {
data: JSON.stringify(options),
description: '',
extension: ''
}
})
tim.sendMessage(message)
};
function dialed() {
const options = {
userId: userID,
userSig: userSig,
roomId: roomID,
sdkAppId: window.genTestUserSig('').SDKAppID
}
initTrtc(options).then(() => {
sendVideoMessage(ACTION.VIDEO_CALL_ACTION_ACCEPTED)
startT = new Date()
})
};
function call() {
const options = {
userId: userID,
userSig: userSig,
roomId: roomID,
sdkAppId: window.genTestUserSig('').SDKAppID
}
startT = new Date();
initTrtc(options).then(() => {
// if (!this.ready) return
// changeState('dialling', true)
// timer = setTimeout(this.timeout, process.env.NODE_ENV === 'development' ? 999999 : 60000) // 开始计时器,开发环境超时时间较长,便于调试
sendVideoMessage(ACTION.VIDEO_CALL_ACTION_DIALING)
})
};
function hangup() {
// this.changeState('calling', false)
Trtc.leave();
end = new Date();
const duration = parseInt((end - start) / 1000);
this.sendVideoMessage(ACTION.VIDEO_CALL_ACTION_HANGUP, duration)
};
function accept() { // 接听电话
// this.changeState('calling', true)
const options = {
userId: userID,
userSig: userSig,
roomId: roomID,
sdkAppId: window.genTestUserSig('').SDKAppID
}
initTrtc(options).then(() => {
sendVideoMessage(ACTION.VIDEO_CALL_ACTION_ACCEPTED)
startT = new Date()
})
};