|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +const _ = require('underscore'); |
3 | 4 | const request = require('./request').request; |
4 | 5 | const AV = require('./av'); |
5 | 6 |
|
@@ -135,4 +136,41 @@ module.exports = AV.Object.extend('_Conversation', /** @lends AV.Conversation.pr |
135 | 136 | } |
136 | 137 | return request('rtm', 'messages', null, 'POST', data, authOptions); |
137 | 138 | }, |
| 139 | + |
| 140 | + /** |
| 141 | + * Send realtime broadcast message to all clients, with this conversation, using HTTP request. |
| 142 | + * |
| 143 | + * @param {String} fromClient Sender's client id. |
| 144 | + * @param {(String|Object)} message The message which will send to conversation. |
| 145 | + * It could be a raw string, or an object with a `toJSON` method, like a |
| 146 | + * realtime SDK's Message object. See more: {@link https://leancloud.cn/docs/realtime_guide-js.html#消息}. |
| 147 | + * @param {Object} [options.pushData] Push data to this message. See more: {@link https://url.leanapp.cn/pushData 推送消息内容}. |
| 148 | + * @param {Object} [options.validTill] The message will valid till this time. |
| 149 | + * @param {AuthOptions} [authOptions] |
| 150 | + * @return {Promise} |
| 151 | + */ |
| 152 | + broadcast: function(fromClient, message, options={}, authOptions={}) { |
| 153 | + if (typeof message.toJSON === 'function') { |
| 154 | + message = message.toJSON(); |
| 155 | + } |
| 156 | + if (typeof message !== 'string') { |
| 157 | + message = JSON.stringify(message); |
| 158 | + } |
| 159 | + const data = { |
| 160 | + from_peer: fromClient, |
| 161 | + conv_id: this.id, |
| 162 | + message: message, |
| 163 | + }; |
| 164 | + if (options.pushData !== undefined) { |
| 165 | + data.push = options.pushData; |
| 166 | + } |
| 167 | + if (options.validTill !== undefined) { |
| 168 | + let ts = options.validTill; |
| 169 | + if (_.isDate(ts)) { |
| 170 | + ts = ts.getTime(); |
| 171 | + } |
| 172 | + options.valid_till = ts; |
| 173 | + } |
| 174 | + return request('rtm', 'broadcast', null, 'POST', data, authOptions); |
| 175 | + } |
138 | 176 | }); |
0 commit comments