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