如何从堆栈溢出获取新问题的通知? [英] How get notifications from stack overflow for new questions?
问题描述
我想回答新的 javascript、react、react-native 和 node 问题.那么,我如何知道用户在这些领域提出的新问题?
I would like to answer to new javascript, react, react-native and node questions. So, how would I know about the new questions which are asked by users on these areas?
推荐答案
打开一个到 wss://qa.sockets.stackexchange.com/
的 Websocket 连接,然后发送消息 1-questions-newest-tag-TAG
其中 TAG
是您要关注的标签.这是一个实时片段:
Open a Websocket connection to wss://qa.sockets.stackexchange.com/
, then send the message 1-questions-newest-tag-TAG
where TAG
is the tag you want to watch for. Here's a live snippet:
const socket = new WebSocket('wss://qa.sockets.stackexchange.com/');
socket.onopen = () => {
socket.send('1-questions-newest-tag-javascript');
socket.send('1-questions-newest-tag-java');
socket.send('1-questions-newest-tag-python');
socket.send('1-questions-newest-tag-php');
console.log('Listening...');
};
const seenQuestions = new Set();
socket.onmessage = ({ data }) => {
const obj = JSON.parse(data);
if (obj.action === 'hb') {
socket.send('pong');
return;
}
const { id, body } = JSON.parse(obj.data);
if (seenQuestions.has(id)) {
// Duplicate question, a message for it has already been handled:
return;
}
seenQuestions.add(id);
console.log('New question:', id);
document.body.insertAdjacentHTML('beforeend', body);
};
socket.onerror = console.error; // just in case
<!-- Makes the inserted questions look a bit prettier: -->
<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=d385c34dc2d7">
您确实需要监听 hb
消息并回复它,以便 StackExchange 知道保持连接有效.
You do need to listen for a hb
message and reply to it, so that StackExchange knows to keep the connection alive.
请注意,套接字将为给定问题的每个被监听的标签发送数据.例如,如果某物同时使用 Javascript 和 React 标记,并且您已发送请求以侦听这两个标记,您将收到一条消息两次,因此需要 Set 来避免列出重复.
Do note that the socket will send data for a given question for every tag being listened for. Eg, if something is tagged with both Javascript and React, and you've sent requests to listen for both tags, you'll receive a message for that twice, hence the need for the Set to avoid listing duplicates.
这篇关于如何从堆栈溢出获取新问题的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!