-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made req and res EventEmitters, added res 'final' event, added req.ip
- Loading branch information
1 parent
2d02512
commit 751ec21
Showing
8 changed files
with
141 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var EventEmitter = require('events'); | ||
var util = require('util'); | ||
|
||
function SocketIoRequest (socket, route, message) { | ||
EventEmitter.call(this); | ||
this.socket = socket; | ||
this.url = route || '/'; | ||
this.originalUrl = this.url; | ||
this.message = message || {}; | ||
this.method = this.message.method && this.message.method.toUpperCase() || 'GET'; | ||
this.headers = this.message.headers || {}; | ||
this.body = this.message.body; | ||
this.ip = this.socket.request.connection.remoteAddress; | ||
} | ||
|
||
util.inherits(SocketIoRequest, EventEmitter); | ||
|
||
SocketIoRequest.prototype.method = 'GET'; | ||
SocketIoRequest.prototype.get = function(field) { | ||
return this.headers && this.headers[field]; | ||
}; | ||
SocketIoRequest.prototype.header = SocketIoRequest.prototype.get; | ||
|
||
module.exports = SocketIoRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var EventEmitter = require('events'); | ||
var util = require('util'); | ||
|
||
function SocketIoResponse (socket, route) { | ||
this.socket = socket; | ||
this.headers = {}; | ||
this.sentResponse = false; | ||
// emitUrl only goes up until the first question mark | ||
this.emitUrl = route || '/'; | ||
var questionMarkIndex = this.emitUrl.indexOf('?'); | ||
if(questionMarkIndex !== -1) { | ||
this.emitUrl = this.emitUrl.substring(0, questionMarkIndex); | ||
} | ||
} | ||
|
||
util.inherits(SocketIoResponse, EventEmitter); | ||
|
||
SocketIoResponse.prototype.statusCode = 200; | ||
|
||
SocketIoResponse.prototype.set = function(field, value) { | ||
this.headers[field] = value; | ||
}; | ||
SocketIoResponse.prototype.header = SocketIoResponse.prototype.set; | ||
|
||
SocketIoResponse.prototype.status = function(code) { | ||
this.statusCode = code; | ||
return this; | ||
}; | ||
|
||
SocketIoResponse.prototype.send = function(body) { | ||
if(this.sentResponse) { | ||
console.warn('WARNING: fresh-socketio-router: send: response has already been sent and will not be sent again'); | ||
return this; | ||
} | ||
var message = { status: this.statusCode, headers: this.headers }; | ||
if(body !== undefined) { | ||
message.body = body; | ||
} | ||
this.socket.emit(this.emitUrl, message); | ||
this.emit('finish'); | ||
this.sentResponse = true; | ||
return this; | ||
}; | ||
|
||
SocketIoResponse.prototype.json = SocketIoResponse.prototype.send; | ||
|
||
module.exports = SocketIoResponse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters