* Sends a data message to the other peer. * * @param {*} data The message to send * @param {Object} options Options object * @param {Boolean} [options.binary=false] Specifies whether `data` is binary * or text * @param {Boolean} [options.compress=false] Specifies whether or not
(data, options, cb)
| 349 | * @public |
| 350 | */ |
| 351 | send(data, options, cb) { |
| 352 | const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName]; |
| 353 | let opcode = options.binary ? 2 : 1; |
| 354 | let rsv1 = options.compress; |
| 355 | |
| 356 | let byteLength; |
| 357 | let readOnly; |
| 358 | |
| 359 | if (typeof data === 'string') { |
| 360 | byteLength = Buffer.byteLength(data); |
| 361 | readOnly = false; |
| 362 | } else if (isBlob(data)) { |
| 363 | byteLength = data.size; |
| 364 | readOnly = false; |
| 365 | } else { |
| 366 | data = toBuffer(data); |
| 367 | byteLength = data.length; |
| 368 | readOnly = toBuffer.readOnly; |
| 369 | } |
| 370 | |
| 371 | if (this._firstFragment) { |
| 372 | this._firstFragment = false; |
| 373 | if ( |
| 374 | rsv1 && |
| 375 | perMessageDeflate && |
| 376 | perMessageDeflate.params[ |
| 377 | perMessageDeflate._isServer |
| 378 | ? 'server_no_context_takeover' |
| 379 | : 'client_no_context_takeover' |
| 380 | ] |
| 381 | ) { |
| 382 | rsv1 = byteLength >= perMessageDeflate._threshold; |
| 383 | } |
| 384 | this._compress = rsv1; |
| 385 | } else { |
| 386 | rsv1 = false; |
| 387 | opcode = 0; |
| 388 | } |
| 389 | |
| 390 | if (options.fin) this._firstFragment = true; |
| 391 | |
| 392 | const opts = { |
| 393 | [kByteLength]: byteLength, |
| 394 | fin: options.fin, |
| 395 | generateMask: this._generateMask, |
| 396 | mask: options.mask, |
| 397 | maskBuffer: this._maskBuffer, |
| 398 | opcode, |
| 399 | readOnly, |
| 400 | rsv1 |
| 401 | }; |
| 402 | |
| 403 | if (isBlob(data)) { |
| 404 | if (this._state !== DEFAULT) { |
| 405 | this.enqueue([this.getBlobData, data, this._compress, opts, cb]); |
| 406 | } else { |
| 407 | this.getBlobData(data, this._compress, opts, cb); |
| 408 | } |