server.js 611 B

1234567891011121314151617181920212223242526272829
  1. 'use strict'
  2. var WebSocketServer = require('ws').Server
  3. var stream = require('./stream')
  4. class Server extends WebSocketServer{
  5. constructor(opts, cb) {
  6. super(opts)
  7. var proxied = false
  8. this.on('newListener', function(event) {
  9. if (!proxied && event === 'stream') {
  10. proxied = true
  11. this.on('connection', function(conn, req) {
  12. this.emit('stream', stream(conn, opts), req)
  13. })
  14. }
  15. })
  16. if (cb) {
  17. this.on('stream', cb)
  18. }
  19. }
  20. }
  21. module.exports.Server = Server
  22. module.exports.createServer = function(opts, cb) {
  23. return new Server(opts, cb)
  24. }