| 6 | module.exports = EngineServer |
| 7 | |
| 8 | function EngineServer(onConnection) { |
| 9 | var engine = new EventEmitter |
| 10 | var servers = [] |
| 11 | |
| 12 | engine.attach = attach |
| 13 | engine.close = close |
| 14 | |
| 15 | if (onConnection) { |
| 16 | engine.on("connection", onConnection) |
| 17 | } |
| 18 | |
| 19 | function attach(httpServer, options) { |
| 20 | options = options || {} |
| 21 | |
| 22 | if (typeof options === "string") { |
| 23 | options = { path: options } |
| 24 | } |
| 25 | |
| 26 | var server = EngineIO.attach(httpServer, options) |
| 27 | |
| 28 | servers.push(server) |
| 29 | |
| 30 | server.on("error", engine.emit.bind(engine, "error")); |
| 31 | |
| 32 | server.on("connection", function (socket) { |
| 33 | engine.emit("connection", EngineStream(socket)) |
| 34 | }) |
| 35 | return server |
| 36 | } |
| 37 | |
| 38 | function close() { |
| 39 | servers.forEach(invoke("close")) |
| 40 | } |
| 41 | |
| 42 | return engine |
| 43 | } |
| 44 | |
| 45 | function invoke(method) { |
| 46 | return function (obj) { |