* The client sends a request awaiting for us to send data. * * @private
(req: EngineRequest, res: ServerResponse)
| 69 | * @private |
| 70 | */ |
| 71 | private onPollRequest(req: EngineRequest, res: ServerResponse) { |
| 72 | if (this.req) { |
| 73 | debug("request overlap"); |
| 74 | // assert: this.res, '.req and .res should be (un)set together' |
| 75 | this.onError("overlap from client"); |
| 76 | res.writeHead(400); |
| 77 | res.end(); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | debug("setting request"); |
| 82 | |
| 83 | this.req = req; |
| 84 | this.res = res; |
| 85 | |
| 86 | const onClose = () => { |
| 87 | this.onError("poll connection closed prematurely"); |
| 88 | }; |
| 89 | |
| 90 | const cleanup = () => { |
| 91 | req.removeListener("close", onClose); |
| 92 | this.req = this.res = null; |
| 93 | }; |
| 94 | |
| 95 | req.cleanup = cleanup; |
| 96 | req.on("close", onClose); |
| 97 | |
| 98 | this.writable = true; |
| 99 | this.emit("ready"); |
| 100 | |
| 101 | // if we're still writable but had a pending close, trigger an empty send |
| 102 | if (this.writable && this.shouldClose) { |
| 103 | debug("triggering empty send to append close packet"); |
| 104 | this.send([{ type: "noop" }]); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * The client sends a request with data. |