How to design a system that supports real-time interaction in online answering questions
With the development of the Internet, online learning has become a common learning method. The emergence of online question answering platforms has made learning more flexible and convenient. However, most of the current online question-answering platforms only provide simple question-answering functions and do not realize real-time interaction. In order to meet students' needs for a richer and more diverse learning experience, we need to design an online question answering system that supports real-time interaction.
In order to achieve this goal, we can use websocket to achieve real-time data transmission, and adopt a front-end and back-end separation architecture. The front-end uses React.js to build the user interface, and the back-end uses Node.js to build the server, and Use MongoDB as database.
First, we need to design a database structure to store questions and answer records. You can create two collections, one to store question information and the other to store answer records. The question collection can contain the following fields:
{ _id: ObjectId, question: String, options: [String], answer: String }
The answer record collection can contain the following fields:
{ _id: ObjectId, userId: String, questionId: ObjectId, answer: String, correct: Boolean }
Next, we need to write a back-end API to provide addition, deletion, modification and query of questions and answer records. Function. For example, we can create the following API interface:
In the front-end part, we can use React.js to create an interactive user interface. Users can browse the list of questions and select answers to submit. We can use websocket to update other users' answers in real time after the user submits the answer.
The following is a simple sample code to display the question list and answer function:
import React, { useState, useEffect } from 'react'; const Quiz = () => { const [questions, setQuestions] = useState([]); const [answered, setAnswered] = useState(false); const [answer, setAnswer] = useState(''); useEffect(() => { fetch('/api/questions') .then(response => response.json()) .then(data => setQuestions(data)); }, []); const handleSubmit = () => { fetch('/api/records', { method: 'POST', body: JSON.stringify({ answer }), headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => { setAnswered(true); // 更新其他用户的答题情况 }); }; return ( <div> <h1>在线答题</h1> {questions.map(question => ( <div key={question._id}> <h3>{question.question}</h3> {question.options.map(option => ( <div key={option}> <input type="radio" id={option} name="answer" value={option} onChange={e => setAnswer(e.target.value)} disabled={answered} /> <label htmlFor={option}>{option}</label> </div> ))} </div> ))} {!answered && ( <button onClick={handleSubmit}>提交答案</button> )} </div> ); }; export default Quiz;
The above code is only an example, and the specific implementation needs to be adjusted and improved according to actual needs.
Through the above design and implementation, we can create an online question answering system that supports real-time interaction. Students can answer questions online in the system and see other students' answers in real time. Such a system can not only make learning more interesting and interactive, but also promote cooperation and thinking collisions among students, improving learning effects.
The above is the detailed content of How to design a system that supports real-time interaction in online question answering. For more information, please follow other related articles on the PHP Chinese website!