| 5 | var BinaryClient = require('./client').BinaryClient; |
| 6 | |
| 7 | function BinaryServer(options) { |
| 8 | if (!(this instanceof BinaryServer)) return new BinaryServer(options); |
| 9 | |
| 10 | var self = this; |
| 11 | |
| 12 | options = util.extend({ |
| 13 | host: '0.0.0.0', |
| 14 | chunkSize: 40960 |
| 15 | }, options); |
| 16 | |
| 17 | this.clients = {}; |
| 18 | this._clientCounter = 0; |
| 19 | |
| 20 | |
| 21 | if (options.server && (options.server instanceof ws.Server)) { |
| 22 | this._server = options.server; |
| 23 | } else { |
| 24 | this._server = new ws.Server(options); |
| 25 | } |
| 26 | |
| 27 | this._server.on('connection', function(socket){ |
| 28 | var clientId = self._clientCounter; |
| 29 | var binaryClient = new BinaryClient(socket, options); |
| 30 | binaryClient.id = clientId; |
| 31 | self.clients[clientId] = binaryClient; |
| 32 | self._clientCounter++; |
| 33 | binaryClient.on('close', function(){ |
| 34 | delete self.clients[clientId]; |
| 35 | }); |
| 36 | self.emit('connection', binaryClient); |
| 37 | }); |
| 38 | this._server.on('error', function(error){ |
| 39 | self.emit('error', error); |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | util.inherits(BinaryServer, EventEmitter); |
| 44 | |