2 * ws: a node.js websocket client
3 * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
7 var events = require('events')
8 , util = require('util')
9 , EventEmitter = events.EventEmitter;
12 * Hixie Sender implementation
15 function Sender(socket) {
17 this.continuationFrame = false;
18 this.isClosed = false;
21 module.exports = Sender;
24 * Inherits from EventEmitter.
27 util.inherits(Sender, events.EventEmitter);
30 * Frames and writes data.
35 Sender.prototype.send = function(data, options, cb) {
36 if (this.isClosed) return;
38 var isString = typeof data == 'string'
39 , length = isString ? Buffer.byteLength(data) : data.length
40 , lengthbytes = (length > 127) ? 2 : 1 // assume less than 2**14 bytes
41 , writeStartMarker = this.continuationFrame == false
42 , writeEndMarker = !options || !(typeof options.fin != 'undefined' && !options.fin)
43 , buffer = new Buffer((writeStartMarker ? ((options && options.binary) ? (1 + lengthbytes) : 1) : 0) + length + ((writeEndMarker && !(options && options.binary)) ? 1 : 0))
44 , offset = writeStartMarker ? 1 : 0;
46 if (writeStartMarker) {
47 if (options && options.binary) {
48 buffer.write('\x80', 'binary');
49 // assume length less than 2**14 bytes
51 buffer.write(String.fromCharCode(128+length/128), offset++, 'binary');
52 buffer.write(String.fromCharCode(length&0x7f), offset++, 'binary');
54 buffer.write('\x00', 'binary');
57 if (isString) buffer.write(data, offset, 'utf8');
58 else data.copy(buffer, offset, 0);
61 if (options && options.binary) {
62 // sending binary, not writing end marker
64 buffer.write('\xff', offset + length, 'binary');
65 this.continuationFrame = false;
67 else this.continuationFrame = true;
70 this.socket.write(buffer, 'binary', cb);
72 this.error(e.toString());
77 * Sends a close instruction to the remote party.
82 Sender.prototype.close = function(code, data, mask, cb) {
83 if (this.isClosed) return;
86 if (this.continuationFrame) this.socket.write(new Buffer([0xff], 'binary'));
87 this.socket.write(new Buffer([0xff, 0x00]), 'binary', cb);
89 this.error(e.toString());
94 * Sends a ping message to the remote party. Not available for hixie.
99 Sender.prototype.ping = function(data, options) {};
102 * Sends a pong message to the remote party. Not available for hixie.
107 Sender.prototype.pong = function(data, options) {};
115 Sender.prototype.error = function (reason) {
116 this.emit('error', reason);