我正在尝试创建一张发票,然后通过订阅事件来跟踪其状态。为此,我使用了bitpay REST API,并且我已经成功地创建了发票并成功地检索了总线令牌。
这是bitpay docs所说的:
Once you have retrieved the bus pass, It's pretty simple. Send a GET request to the path configured for the bus - let's say it's bitpay.com/events including the appropriate parameters:
?token= - your "bus pass" retrieved from the API
&action= - usually "subscribe", but options are provided in your bus pass
&events[]= - an array of events to listen for (also provided in the bus pass)
Once you've opened the connection, you'll want to listen for chunks of data to be passed to you. Each chunk represents an event. Since the bus adheres to the SSE/EventSource specification, chunks are passed back in the format:
event: <event_type>
data: {"some":"json"}如何收听这些事件?当我点击我正在尝试收听的链接时,我看到的是:
:
retry: 2000
event: connect
data: {"status":"subscribed","resource":"Sg9hggVxgC1VTKsGYqNyVT","facade":"public/invoice","events":["payment","confirmation","paymentRejected","scanned","paymentPosted"]}
event: state
data: {"url":"https://test.bitpay.com/invoice?
...Herre是我尝试过的:
const evtSource = new EventSource('<path>');
evtSource.onmessage = (e) => {
console.log(e);
}和
axios({
url : '<path>',
method: 'get',
connect: function(event){
console.log(event);
}
})谢谢!
编辑:这是一个我想听的link示例。
发布于 2020-12-07 23:29:58
以下是Codesandbox演示https://codesandbox.io/s/gifted-borg-365z2?file=/app.js:0-496的工作示例
const EventSource = require("eventsource");
const eventSourceInitDict = { https: { rejectUnauthorized: false } };
const evtSource = new EventSource(
"https://test.bitpay.com/events?token=767cdhmwtn7XgW1QrSkuEweXC3dUyCRcZqwvcGRvSeaDZh8UXj9aCcS1xPPppjaFYH&action=subscribe&events[]=payment&events[]=confirmation&events[]=paymentRejected&events[]=scanned&events[]=paymentPosted",
eventSourceInitDict
);
evtSource.addEventListener("connect", function (e) {
console.log("connect", e.data);
});
LOGS: connect {"status":"subscribed","resource":"HZxbCd5YRWnWJ4kK72DBai","facade":"public/invoice","events":["payment","confirmation","paymentRejected","scanned","paymentPosted"]因此,我猜在连接事件之后,您可以订阅其他事件,如下所示:
evtSource.addEventListener("payment", function (e) {
console.log("payment", e.data);
});尝试而不是:
const evtSource = new EventSource('<path>');
evtSource.onmessage = (e) => {
console.log(e);
}更新:https://developer.mozilla.org/en-US/docs/Web/API/EventSource
const sse = new EventSource('<path>');
sse.addEventListener('<event>', function(e) {
console.log(e.data)
});https://stackoverflow.com/questions/65184193
复制相似问题