Skip to content

Commit

Permalink
Bumped ws to v2.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Feb 8, 2017
1 parent 92871eb commit 138605d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
2 changes: 2 additions & 0 deletions echo-server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

var http = require('http')
var websocket = require('./')
var server = null
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var Server = require('./server.js')
var server = require('./server.js')

module.exports = require('./stream.js')
module.exports.Server = Server
module.exports.createServer = Server
module.exports.Server = server.Server
module.exports.createServer = server.createServer
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"duplexify": "^3.2.0",
"inherits": "^2.0.1",
"through2": "^2.0.0",
"ws": "^1.0.1",
"ws": "^2.0.0",
"xtend": "^4.0.0"
},
"devDependencies": {
Expand Down
44 changes: 21 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
'use strict'

var inherits = require('inherits')
var WebSocketServer = require('ws').Server
var stream = require('./stream')

module.exports = Server

function Server(opts, cb) {
if (!(this instanceof Server)) {
return new Server(opts, cb)
}

WebSocketServer.call(this, opts)

var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn) {
this.emit('stream', stream(conn, opts))
})
class Server extends WebSocketServer{
constructor(opts, cb) {
super(opts)

var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn) {
this.emit('stream', stream(conn, opts))
})
}
})

if (cb) {
this.on('stream', cb)
}
})

if (cb) {
this.on('stream', cb)
}
}

inherits(Server, WebSocketServer)

module.exports.Server = Server
module.exports.createServer = function(opts, cb) {
return new Server(opts, cb)
}
9 changes: 8 additions & 1 deletion stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function WebSocketStream(target, protocols, options) {
if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) {
// accept the "options" Object as the 2nd argument
options = protocols
protocols = null
protocols = []

if (typeof options.protocol === 'string' || Array.isArray(options.protocol)) {
protocols = options.protocol;
Expand Down Expand Up @@ -67,6 +67,13 @@ function WebSocketStream(target, protocols, options) {
var coerceToBuffer = options.binary || options.binary === undefined

function socketWriteNode(chunk, enc, next) {
// avoid errors, this never happens unless
// destroy() is called
if (socket.readyState !== WS.OPEN) {
next()
return
}

if (coerceToBuffer && typeof chunk === 'string') {
chunk = new Buffer(chunk, 'utf8')
}
Expand Down

0 comments on commit 138605d

Please sign in to comment.