| 1 | /*! |
| 2 | * ws: a node.js websocket client |
| 3 | * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> |
| 4 | * MIT Licensed |
| 5 | */ |
| 6 | |
| 7 | var events = require('events') |
| 8 | , util = require('util') |
| 9 | , EventEmitter = events.EventEmitter; |
| 10 | |
| 11 | /** |
| 12 | * Hixie Sender implementation |
| 13 | */ |
| 14 | |
| 15 | function Sender(socket) { |
| 16 | if (this instanceof Sender === false) { |
| 17 | throw new TypeError("Classes can't be function-called"); |
| 18 | } |
| 19 | |
| 20 | events.EventEmitter.call(this); |
| 21 | |
| 22 | this.socket = socket; |
| 23 | this.continuationFrame = false; |
| 24 | this.isClosed = false; |
| 25 | } |
| 26 | |
| 27 | module.exports = Sender; |
| 28 | |
| 29 | /** |
| 30 | * Inherits from EventEmitter. |
| 31 | */ |
| 32 | |
| 33 | util.inherits(Sender, events.EventEmitter); |
| 34 | |
| 35 | /** |
| 36 | * Frames and writes data. |
| 37 | * |
| 38 | * @api public |
| 39 | */ |
| 40 | |
| 41 | Sender.prototype.send = function(data, options, cb) { |
| 42 | if (this.isClosed) return; |
| 43 | |
| 44 | var isString = typeof data == 'string' |
| 45 | , length = isString ? Buffer.byteLength(data) : data.length |
| 46 | , lengthbytes = (length > 127) ? 2 : 1 // assume less than 2**14 bytes |
| 47 | , writeStartMarker = this.continuationFrame == false |
| 48 | , writeEndMarker = !options || !(typeof options.fin != 'undefined' && !options.fin) |
| 49 | , buffer = new Buffer((writeStartMarker ? ((options && options.binary) ? (1 + lengthbytes) : 1) : 0) + length + ((writeEndMarker && !(options && options.binary)) ? 1 : 0)) |
| 50 | , offset = writeStartMarker ? 1 : 0; |
| 51 | |
| 52 | if (writeStartMarker) { |
| 53 | if (options && options.binary) { |
| 54 | buffer.write('\x80', 'binary'); |
| 55 | // assume length less than 2**14 bytes |
| 56 | if (lengthbytes > 1) |
| 57 | buffer.write(String.fromCharCode(128+length/128), offset++, 'binary'); |
| 58 | buffer.write(String.fromCharCode(length&0x7f), offset++, 'binary'); |
| 59 | } else |
| 60 | buffer.write('\x00', 'binary'); |
| 61 | } |
| 62 | |
| 63 | if (isString) buffer.write(data, offset, 'utf8'); |
| 64 | else data.copy(buffer, offset, 0); |
| 65 | |
| 66 | if (writeEndMarker) { |
| 67 | if (options && options.binary) { |
| 68 | // sending binary, not writing end marker |
| 69 | } else |
| 70 | buffer.write('\xff', offset + length, 'binary'); |
| 71 | this.continuationFrame = false; |
| 72 | } |
| 73 | else this.continuationFrame = true; |
| 74 | |
| 75 | try { |
| 76 | this.socket.write(buffer, 'binary', cb); |
| 77 | } catch (e) { |
| 78 | this.error(e.toString()); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Sends a close instruction to the remote party. |
| 84 | * |
| 85 | * @api public |
| 86 | */ |
| 87 | |
| 88 | Sender.prototype.close = function(code, data, mask, cb) { |
| 89 | if (this.isClosed) return; |
| 90 | this.isClosed = true; |
| 91 | try { |
| 92 | if (this.continuationFrame) this.socket.write(new Buffer([0xff], 'binary')); |
| 93 | this.socket.write(new Buffer([0xff, 0x00]), 'binary', cb); |
| 94 | } catch (e) { |
| 95 | this.error(e.toString()); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * Sends a ping message to the remote party. Not available for hixie. |
| 101 | * |
| 102 | * @api public |
| 103 | */ |
| 104 | |
| 105 | Sender.prototype.ping = function(data, options) {}; |
| 106 | |
| 107 | /** |
| 108 | * Sends a pong message to the remote party. Not available for hixie. |
| 109 | * |
| 110 | * @api public |
| 111 | */ |
| 112 | |
| 113 | Sender.prototype.pong = function(data, options) {}; |
| 114 | |
| 115 | /** |
| 116 | * Handles an error |
| 117 | * |
| 118 | * @api private |
| 119 | */ |
| 120 | |
| 121 | Sender.prototype.error = function (reason) { |
| 122 | this.emit('error', reason); |
| 123 | return this; |
| 124 | }; |