首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用NodeJs监听和订阅HTTP事件?

如何使用NodeJs监听和订阅HTTP事件?
EN

Stack Overflow用户
提问于 2020-12-07 23:06:15
回答 1查看 357关注 0票数 1

我正在尝试创建一张发票,然后通过订阅事件来跟踪其状态。为此,我使用了bitpay REST API,并且我已经成功地创建了发票并成功地检索了总线令牌。

这是bitpay docs所说的:

代码语言:javascript
复制
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"}

如何收听这些事件?当我点击我正在尝试收听的链接时,我看到的是:

代码语言:javascript
复制
:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
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是我尝试过的:

代码语言:javascript
复制
const evtSource = new EventSource('<path>');

evtSource.onmessage = (e) => {
    console.log(e);
}

代码语言:javascript
复制
axios({
    url : '<path>',
    method: 'get',
    connect: function(event){
        console.log(event);
    }
})

谢谢!

编辑:这是一个我想听的link示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-07 23:29:58

以下是Codesandbox演示https://codesandbox.io/s/gifted-borg-365z2?file=/app.js:0-496的工作示例

代码语言:javascript
复制
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"]

因此,我猜在连接事件之后,您可以订阅其他事件,如下所示:

代码语言:javascript
复制
evtSource.addEventListener("payment", function (e) {
  console.log("payment", e.data);
});

尝试而不是:

代码语言:javascript
复制
const evtSource = new EventSource('<path>');
evtSource.onmessage = (e) => {
    console.log(e);
}

更新:https://developer.mozilla.org/en-US/docs/Web/API/EventSource

代码语言:javascript
复制
  const sse = new EventSource('<path>');
  sse.addEventListener('<event>', function(e) { 
    console.log(e.data)
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65184193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档