simple-websocket
Advanced tools
Comparing version
40
index.js
@@ -1,2 +0,2 @@ | ||
/* global WebSocket */ | ||
/* global WebSocket, DOMException */ | ||
@@ -70,6 +70,3 @@ module.exports = Socket | ||
} catch (err) { | ||
process.nextTick(function () { | ||
self._destroy(err) | ||
}) | ||
return | ||
return self.destroy(err) | ||
} | ||
@@ -89,3 +86,3 @@ } | ||
self._ws.onerror = function () { | ||
self._destroy(new Error('connection error to ' + self.url)) | ||
self.destroy(new Error('connection error to ' + self.url)) | ||
} | ||
@@ -109,10 +106,23 @@ | ||
Socket.prototype.destroy = function (onclose) { | ||
this._destroy(null, onclose) | ||
// TODO: Delete this method once readable-stream is updated to contain a default | ||
// implementation of destroy() that automatically calls destroy() | ||
// See: https://github.com/nodejs/readable-stream/issues/283 | ||
Socket.prototype.destroy = function (err) { | ||
var self = this | ||
self._destroy(err, function (err) { | ||
if (err) { | ||
if (typeof DOMException !== 'undefined' && err instanceof DOMException) { | ||
// Convert Edge DOMException object to Error object | ||
var code = err.code | ||
err = new Error(err.message) | ||
err.code = code | ||
} | ||
process.nextTick(function () { self.emit('error', err) }) | ||
} | ||
}) | ||
} | ||
Socket.prototype._destroy = function (err, onclose) { | ||
Socket.prototype._destroy = function (err, cb) { | ||
var self = this | ||
if (self.destroyed) return | ||
if (onclose) self.once('close', onclose) | ||
@@ -158,3 +168,3 @@ self._debug('destroy (error: %s)', err && (err.message || err)) | ||
if (err) self.emit('error', err) | ||
cb(err) | ||
self.emit('close') | ||
@@ -172,3 +182,3 @@ } | ||
} catch (err) { | ||
return this._destroy(err) | ||
return this.destroy(err) | ||
} | ||
@@ -204,3 +214,3 @@ if (typeof ws !== 'function' && this._ws.bufferedAmount > MAX_BUFFERED_AMOUNT) { | ||
setTimeout(function () { | ||
self._destroy() | ||
self.destroy() | ||
}, 1000) | ||
@@ -226,3 +236,3 @@ } | ||
} catch (err) { | ||
return self._destroy(err) | ||
return self.destroy(err) | ||
} | ||
@@ -263,3 +273,3 @@ self._chunk = null | ||
this._debug('on close') | ||
this._destroy() | ||
this.destroy() | ||
} | ||
@@ -266,0 +276,0 @@ |
{ | ||
"name": "simple-websocket", | ||
"description": "Simple, EventEmitter API for WebSockets (browser)", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"author": { | ||
@@ -25,3 +25,3 @@ "name": "Feross Aboukhadijeh", | ||
"devDependencies": { | ||
"browserify": "^15.2.0", | ||
"browserify": "^16.1.0", | ||
"prettier-bytes": "^1.0.3", | ||
@@ -28,0 +28,0 @@ "speedometer": "^1.0.0", |
@@ -56,4 +56,6 @@ # simple-websocket [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] | ||
Note: If you're **NOT** using browserify, then use the standalone `simplewebsocket.min.js` | ||
file included in this repo. This exports a `SimpleWebsocket` function on the `window`. | ||
**Note:** If you're **NOT** using browserify, then use the included standalone file | ||
`simplewebsocket.min.js`. This exports a `SimpleWebsocket` constructor on `window`. | ||
Wherever you see `Socket` in the examples below, substitute that with | ||
`SimpleWebsocket`. | ||
@@ -65,3 +67,3 @@ ## api | ||
Create a new WebSocket connection to the server at `url`. This usage is a shorthand | ||
for `socket = new Socket({ url: url }) | ||
for `socket = new Socket({ url: url })` | ||
@@ -89,7 +91,8 @@ ### `socket = new Socket(opts)` | ||
### `socket.destroy([onclose])` | ||
### `socket.destroy([err])` | ||
Destroy and cleanup this websocket connection. | ||
If the optional `onclose` paramter is passed, then it will be registered as a listener on the 'close' event. | ||
If the optional `err` paramter is passed, then it will be emitted as an `'error'` | ||
event on the stream. | ||
@@ -96,0 +99,0 @@ ### `Socket.WEBSOCKET_SUPPORT` |
@@ -11,4 +11,4 @@ var Socket = require('../') | ||
test('create socket without options', function (t) { | ||
t.plan(1) | ||
test('create invalid socket', function (t) { | ||
t.plan(2) | ||
@@ -21,4 +21,4 @@ var socket | ||
t.ok(err instanceof Error, 'got error') | ||
socket.destroy() | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -37,5 +37,6 @@ | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -58,5 +59,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -77,5 +79,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -98,5 +101,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -117,7 +121,8 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
}) | ||
}) |
@@ -25,5 +25,6 @@ /* global WebSocket */ | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -47,5 +48,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -71,5 +73,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -93,7 +96,8 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
}) | ||
}) |
@@ -20,5 +20,6 @@ var Socket = require('../') | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -42,5 +43,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -66,5 +68,6 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
@@ -88,7 +91,8 @@ }) | ||
socket.destroy(function () { | ||
socket.on('close', function () { | ||
t.pass('destroyed socket') | ||
}) | ||
socket.destroy() | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
97413
4.61%876
4.16%156
1.96%