Skip to content

Commit

Permalink
using ajax to set websocket clinet's IP
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Mar 24, 2013
1 parent a2b16d1 commit b6a8f8d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
33 changes: 30 additions & 3 deletions examples/clients/web/actionHeroWebSocket.js
Expand Up @@ -33,7 +33,8 @@
path: "/faye", path: "/faye",
setupChannel: "/_welcome", setupChannel: "/_welcome",
channelPrefix: "/client/websocket/connection/", channelPrefix: "/client/websocket/connection/",
startupDelay: 500 startupDelay: 500,
apiPath: "/api"
} }
} }


Expand Down Expand Up @@ -64,8 +65,10 @@
}); });


setTimeout(function(){ setTimeout(function(){
self.detailsView(function(details){ self.setIP(function(err, ip){
callback(null, details); self.detailsView(function(details){
callback(null, details);
});
}); });
}, self.options.startupDelay); }, self.options.startupDelay);


Expand Down Expand Up @@ -109,6 +112,30 @@
} }
}; };


actionHeroWebSocket.prototype.setIP = function(callback){
var self = this;
try{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var response = JSON.parse(xmlhttp.responseText);
var ip = response.requestorInformation.remoteAddress;
self.send({ event: 'setIP', ip: ip }, function(){
callback(null, ip);
});
}
}
xmlhttp.open("GET", self.options.host + self.options.apiPath, true);
xmlhttp.send();
}catch(e){
// can't make the ajax call, assume it's localhost
var ip = "127.0.0.1";
self.send({ event: 'setIP', ip: ip }, function(){
callback(null, ip);
});
}
}

actionHeroWebSocket.prototype.action = function(action, params, callback){ actionHeroWebSocket.prototype.action = function(action, params, callback){
if(callback == null && typeof params == 'function'){ if(callback == null && typeof params == 'function'){
callback = params; callback = params;
Expand Down
2 changes: 1 addition & 1 deletion examples/clients/web/webSockets.html
Expand Up @@ -2,7 +2,7 @@
<script src="/public/javascript/actionHeroWebSocket.js"></script> <script src="/public/javascript/actionHeroWebSocket.js"></script>


<h1>Websocket Example</h1> <h1>Websocket Example</h1>
Open the Console. Open the Console for more information.


<script> <script>


Expand Down
7 changes: 7 additions & 0 deletions initializers/webSocketServer.js
Expand Up @@ -147,6 +147,12 @@ var webSocketServer = function(api, next){
actionProcessor.processAction(); actionProcessor.processAction();
} }


else if(event == "setIP"){
connection.remoteIP = data.ip;
connection.sendMessage({context: "response", status: "OK", messageCount: connection.messageCount});
api.log("setIP @ webSocket", "debug", {clientId: connection.rawConnection.clientId, params: JSON.stringify(data)});
}

else if(event == "say"){ else if(event == "say"){
api.chatRoom.socketRoomBroadcast(connection, data.message); api.chatRoom.socketRoomBroadcast(connection, data.message);
connection.sendMessage({context: "response", status: "OK", messageCount: connection.messageCount}); connection.sendMessage({context: "response", status: "OK", messageCount: connection.messageCount});
Expand Down Expand Up @@ -199,6 +205,7 @@ var webSocketServer = function(api, next){
var details = { var details = {
params: connection.params, params: connection.params,
id: connection.id, id: connection.id,
remoteIP: connection.remoteIP,
connectedAt: connection.connectedAt, connectedAt: connection.connectedAt,
room: connection.room, room: connection.room,
totalActions: connection.totalActions, totalActions: connection.totalActions,
Expand Down
5 changes: 3 additions & 2 deletions test/client_webSockets.js
Expand Up @@ -71,9 +71,10 @@ describe('Client: Web Sockets', function(){
it('I can get my connection details', function(done){ it('I can get my connection details', function(done){
client_1.detailsView(function(response){ client_1.detailsView(function(response){
response.should.be.an.instanceOf(Object); response.should.be.an.instanceOf(Object);
response.status.should.equal("OK") response.status.should.equal("OK");
response.details.connectedAt.should.be.within(0, new Date().getTime()) response.details.connectedAt.should.be.within(0, new Date().getTime())
response.details.room.should.equal("defaultRoom") response.details.room.should.equal("defaultRoom");
response.details.remoteIP.should.equal("127.0.0.1");
done() done()
}); });
}); });
Expand Down

0 comments on commit b6a8f8d

Please sign in to comment.