Skip to content

Commit 56a3676

Browse files
committed
Add Connection#send
Fixes #27
1 parent 86d91e8 commit 56a3676

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

Connection.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ Connection.prototype.sendBinary = function (data, callback) {
164164
this.emit('error', new Error('You can\'t write to a non-open connection'))
165165
}
166166

167+
/**
168+
* Sends a text or binary frame
169+
* @param {string|Buffer} data
170+
* @param {Function} [callback] will be executed when the data is finally written out
171+
*/
172+
Connection.prototype.send = function (data, callback) {
173+
if (typeof data === 'string') {
174+
this.sendText(data, callback)
175+
} else if (Buffer.isBuffer(data)) {
176+
this.sendBinary(data, callback)
177+
} else {
178+
throw new TypeError('data should be either a string or a Buffer instance')
179+
}
180+
}
181+
167182
/**
168183
* Sends a ping to the remote
169184
* @param {string} [data=''] - optional ping data

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.5.0
2+
* Added: `Connection#send` as a short hand for `Connection#sendText` or `Connection#sendBinary`, depending on the data type (string or Buffer)
3+
14
# 1.4.1
25
* Added: example to README
36

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ Sends a single chunk of binary data (like calling connection.beginBinary().end(d
115115

116116
`callback` will be added as a listener to write operation over the socket
117117

118+
## connection.send(data, [callback])
119+
Sends a given string or Buffer to the other side. This is simply an alias for `sendText()` if data is a string or `sendBinary()` if the data is a Buffer.
120+
121+
`callback` will be added as a listener to write operation over the socket
122+
118123
## connection.sendPing([data=''])
119124
Sends a [ping](http://tools.ietf.org/html/rfc6455#section-5.5.2) with optional payload
120125

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-websocket",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"author": "Sitegui <sitegui@sitegui.com.br>",
55
"description": "Basic server&client approach to websocket (text and binary frames)",
66
"main": "./index.js",

test/test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('extraHeaders', function () {
3535
})
3636
})
3737

38-
describe('text frames', function () {
38+
describe('frames', function () {
3939
before(function (done) {
4040
// Create a test server and one client
4141
testServer = ws.createServer(function (conn) {
@@ -160,6 +160,31 @@ describe('text frames', function () {
160160
done()
161161
})
162162
})
163+
164+
it('should send text and binary data', function (done) {
165+
var client = getClient(),
166+
expected = 'text frame',
167+
textData = 'text data',
168+
binaryData = new Buffer('binary data')
169+
170+
// Use send() for text and binary
171+
client.send(textData)
172+
client.send(binaryData)
173+
174+
// Test whether both were received
175+
getServer(function (str) {
176+
expected.should.be.equal('text frame')
177+
expected = 'binary frame'
178+
str.should.be.equal(textData)
179+
}, function (inStream) {
180+
expected.should.be.equal('binary frame')
181+
expected = ''
182+
inStream.once('readable', function () {
183+
compareBuffers(inStream.read(), binaryData)
184+
})
185+
inStream.on('end', done)
186+
})
187+
})
163188
})
164189

165190
describe('handshake', function () {

0 commit comments

Comments
 (0)