Chat Room Quick Answer Feature Guide
Chat Room Quick Answer Feature Guide
This guide primarily explains how to use the quick answer feature in the chat room from both the viewer and instructor perspectives.
1. When connecting to the chat room, the token and version parameters are required.
Click to get the token documentation
Chat room connection instructions:
io.connect('ws://chat.polyv.net', {
'query': {
version: '2.0', // version必须大于等于2.0
token, // token通过接口获取,看上面的链接
},
'transports': ['websocket']
});
2. Listen for the start quick answer event
- START_ANSWER_QUICKLY
Parameter Description
| Parameter Name | Parameter Description | Parameter Type |
|---|---|---|
| EVENT | Event name, value is START_ANSWER_QUICKLY | string |
| limitTime | Quick answer time limit | number |
| sessionId | Session ID of the live stream | string |
| timestamp | Timestamp when the quick answer was initiated | number |
| quicklyId | Quick answer ID | string |
| overTime | Remaining time for the quick answer | number |
Example
socket.on('message', function(jsonData, callback) {
const data = JSON.parse(jsonData);
if(data.EVENT === 'START_ANSWER_QUICKLY') {
// 执行相关操作
}
})
3. Student submits quick answer information
- SUBMIT_ANSWER_QUICKLY
Parameter Description
| Parameter Name | Parameter Description | Parameter Type | Required |
|---|---|---|---|
| EVENT | Event name, fixed as SUBMIT_ANSWER_QUICKLY | string | Yes |
| quicklyId | Quick answer ID | string | Yes |
| useTime | Time used for the quick answer | number | Yes |
| organization | Student's organization | string | No |
Example
// 需要发送的参数,为json字符串
const sendData = JSON.stringify({
EVENT: 'SUBMIT_ANSWER_QUICKLY',
quicklyId: '436499f0-17d9-11ea-9ba9-6f8eb568fbcb',
useTime: 2
});
socket.emit('message', sendData , function(d) {
const { code, message } = JSON.parse(d); // code 为 请求结果状态
if (code === 200) {
console.log('抢答成功');
} else {
console.log(`抢答失败,失败原因:${message}`)
}
});
4. Get quick answer status
When a quick answer starts, different clients may receive the socket message at different times. For example, the PC client might receive the start quick answer socket before the mobile client, causing a time discrepancy. Therefore, it is necessary to continuously fetch the remaining time from the server to ensure accuracy.
It is recommended to fetch once per second.
GET_ANSWER_QUICKLY_STATUS
Parameter Description
| Parameter Name | Parameter Description | Parameter Type |
|---|---|---|
| EVENT | Event name, fixed as GET_ANSWER_QUICKLY_STATUS | string |
Example
socket.emit(
'message',
JSON.stringify({EVENT:'GET_ANSWER_QUICKLY_STATUS'}),
function(d) {
const { code, data, message } = JSON.parse(d);
if (code === 200) {
const { overTime, answerNum } = data; // overTime为抢答剩余时间,answerNum为已抢答人数
} else {
console.log(`获取抢答状态失败,失败原因:${message}`)
}
}
)
5. Instructor sends the start quick answer event
The instructor clicks the quick answer icon on the streaming client, a window pops up, enters the time, and clicks start quick answer.
- START_ANSWER_QUICKLY
Parameter Description
| Parameter Name | Parameter Description | Parameter Type |
|---|---|---|
| EVENT | Event name, fixed as START_ANSWER_QUICKLY | string |
| limitTime | Quick answer time limit | number |
| sessionId | Session ID of the live stream (for testing, a random string can be passed) | string |
Example
socket.emit('message', JSON.stringify({
EVENT: 'START_ANSWER_QUICKLY',
limitTime: 30,
sessionId: 'test'
}), function(d) {
const { code, message } = JSON.parse(d);
if (code === 200) {
console.log('发起抢答成功');
} else {
console.log(`发起抢答失败,失败原因:${message}`);
}
});
6. End the quick answer
After the instructor initiates the quick answer, there is a stop quick answer button on the page. The instructor can proactively stop the quick answer early.
- STOP_ANSWER_QUICKLY
Parameter Description
| Parameter Name | Parameter Description | Parameter Type |
|---|---|---|
| EVENT | Event name, fixed as STOP_ANSWER_QUICKLY | string |
| quicklyId | Quick answer ID | string |
Example
socket.emit('message', JSON.stringify({
EVENT: 'STOP_ANSWER_QUICKLY',
quicklyId: 'test',
}), function(d) {
const { code, message } = JSON.parse(d);
if (code === 200) {
console.log('抢答停止成功成功');
} else {
console.log(`抢答结束失败,失败原因:${message}`);
}
});
