Skip to content

Commit

Permalink
v0.3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory committed Nov 23, 2022
1 parent 1dab5f9 commit 9287c78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ new Tmc('your_app_key', 'your_app_secret')

**`tmc.send(msg: Message, options?: { mask?: true, binary?: true }, cb?: (err: Error) => void) => void`**

实例化后,当自动应答确认消息无法满足需求的时候,比如消息处理失败,需要`Publisher`再次重推消息,在实例初始化时置`options.autoReplyConfirmation=false`,则在消息处理函数内,可以通过 `this.send()` 函数回复`确认`或者`失败`消息。例如:
`v0.3.4`,当自动应答确认消息无法满足需求的时候,比如消息处理失败,需要`Publisher`再次重推消息,在实例初始化时置`options.autoReplyConfirmation=false`,则在消息处理函数内,可以通过 `this.send()` 函数回复`确认`或者`失败`消息。例如:

```js
new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
Expand All @@ -91,6 +91,29 @@ new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
.connect();
```
也可以使用此方法发送`To淘宝`消息,`>=v0.3.4 && <v0.3.7`版本,需要自主对`$.content.content`数据做`JSON`字符串化(**特别注意:**原生`JSON``BigInt`类型无法处理)。自`v0.3.7`起,可直接对`$.content.content`以原生数据类型描述。例如:
```js
/** @see https://open.taobao.com/tmc.htm?docId=732&docType=9 */
.send(
new Mesage(MessageType.SEND, MessageKind.Data)
.with(MessageFields.TOPIC, 'taobao_fuwu_ElectronicInvoice')
.with(MessageFields.CONTENT, {
id: 12345678901234567n, // 支持 BigInt 类型
tid: 12345678901234567n,
oid: 12345678901234567n,
invoice_file: 12345678901234567n,
e_invoice_no: '12342243435466',
invoice_time: '2015-04-10 10:33:49', // 时区 +08:00
invoice_no: '123456',
invoice_code: '123456',
amount: '100.00',
})
// >=v0.3.4 && <v0.3.7 写法
// .with(MessageFields.CONTENT, '{"id":12345678901234567,"tid":12345678901234567,"amount":"100.00"}')
)
```
<details><summary>可选设置的 NODE_DEBUG=< label > 环境变量</summary>
| label | 说明 |
Expand Down
10 changes: 8 additions & 2 deletions lib/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ class TaoMessageConsumer extends EventEmitter {
* @param {object|Function} [options] - The options
* @param {Function} [cb] - The Callback
* @returns {void}
* @since v0.3.4
* @since v0.3.4 Exposed for `send(new Message(SENDACK, Failed))` scene
* @since v0.3.7 The `data.content.content` can be a plain `object` now
*/
send(data, options = { mask: true, binary: true }, cb = undefined) {
if (data instanceof Message) { this[kWebSocket]?.send(data.with(TOKEN, this[kToken]).buffer, options, cb); }
if (data instanceof Message) {
if (typeof data[CONTENT][CONTENT] === 'object' && data[CONTENT][CONTENT] !== null) {
Reflect.set(data[CONTENT], CONTENT, stringify(data[CONTENT][CONTENT]));
}
this[kWebSocket]?.send(data.with(TOKEN, this[kToken]).buffer, options, cb);
}
}

onopen() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tmc.js",
"version": "0.3.6",
"version": "0.3.7",
"description": "Events driven and chained Taobao Message Channel(TMC) for NodeJS",
"author": "James ZHANG",
"license": "MIT",
Expand Down

0 comments on commit 9287c78

Please sign in to comment.