Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

using ajax to set websocket clinet's IP

  • Loading branch information...
commit b6a8f8d7696d40f125d610c377bb5adb0e948fbf 1 parent a2b16d1
@evantahler authored
View
33 examples/clients/web/actionHeroWebSocket.js
@@ -33,7 +33,8 @@
path: "/faye",
setupChannel: "/_welcome",
channelPrefix: "/client/websocket/connection/",
- startupDelay: 500
+ startupDelay: 500,
+ apiPath: "/api"
}
}
@@ -64,8 +65,10 @@
});
setTimeout(function(){
- self.detailsView(function(details){
- callback(null, details);
+ self.setIP(function(err, ip){
+ self.detailsView(function(details){
+ callback(null, details);
+ });
});
}, self.options.startupDelay);
@@ -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){
if(callback == null && typeof params == 'function'){
callback = params;
View
2  examples/clients/web/webSockets.html
@@ -2,7 +2,7 @@
<script src="/public/javascript/actionHeroWebSocket.js"></script>
<h1>Websocket Example</h1>
-Open the Console.
+Open the Console for more information.
<script>
View
7 initializers/webSocketServer.js
@@ -147,6 +147,12 @@ var webSocketServer = function(api, next){
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"){
api.chatRoom.socketRoomBroadcast(connection, data.message);
connection.sendMessage({context: "response", status: "OK", messageCount: connection.messageCount});
@@ -199,6 +205,7 @@ var webSocketServer = function(api, next){
var details = {
params: connection.params,
id: connection.id,
+ remoteIP: connection.remoteIP,
connectedAt: connection.connectedAt,
room: connection.room,
totalActions: connection.totalActions,
View
5 test/client_webSockets.js
@@ -71,9 +71,10 @@ describe('Client: Web Sockets', function(){
it('I can get my connection details', function(done){
client_1.detailsView(function(response){
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.room.should.equal("defaultRoom")
+ response.details.room.should.equal("defaultRoom");
+ response.details.remoteIP.should.equal("127.0.0.1");
done()
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.