Could someone explain to me how the "notification" from the server back to the browser works.
I can see that the signal comes from [SockJS] ebba.telldus.com:8001 (http://ebba.telldus.com:8001/ws/1, http://ebba.telldus.com:8001/ws/1/info)
and that Telldus Live page uses it to get the information pushed to it, in case a lamp was switched on by the remote, thus eliminating the need of constantly refreshing the status of all the devices and sensors.
So, how does this happen? I am new to Socks communication, I looked at the part of wsevent.js:
Code: Select all
Y.WSEvent.prototype._addConnection = function(id, sockjsaddress, phpsess) {
var _this = this;
if (sockjsaddress == '') {
setTimeout(function() {_this._retryConnection(id);}, 120000);
return;
}
if (_this.connections[id]) {
return; //a connection to this client already exists
}
var connection = new Y.WSConnection(sockjsaddress, id, phpsess);
connection.on('connect', function() {
auth = {'sessionid': phpsess, 'clientId': id}
this.sendMessage('auth', 'auth', auth);
filter = _this.filter;
for(i in filter) {
this.sendMessage('filter', 'accept', filter[i]);
}
});
connection.on('message', function(module, action, data) {
_this.fire('message', module, action, data);
});
connection.on('validconnection', function() {
_this.fire('clientOnline', id);
});
connection.on('connectionlost', function() {
_this.fire('websocketOffline', id);
if (_this.connections[id]) {
_this.connections[id] = null;
}
setTimeout(function() {_this._retryConnection(id);}, 10000);
});
this.connections[id] = connection;
}
Y.WSEvent.prototype._retryConnection = function(id) {
var _this = this;
if(Y.Array.indexOf(_this._isWaitingToRetry, id) && _this._isWaitingToRetry[id]){
//already waiting to retry, don't flood it
return;
}
_this._isWaitingToRetry[id] = true;
Y.io('/wsevent/clientAddress?id=' + id, { method:'GET', on:{ complete:function(ev,o) {
try{
var data = Y.JSON.parse(o.responseText);
if(data.sockjsaddress == ''){
_this.fire('clientOffline', data.clientId);
}
} catch(e){
setTimeout(function() {_this._isWaitingToRetry[id]=false;_this._retryConnection(id);}, 300000); // Retry conenction every 5th minute so see if it's online again.
return;
}
_this._isWaitingToRetry[id] = false;
_this._addConnection(data.clientId, data.sockjsaddress, data.phpsess);
} }});
}
Y.WSEvent.prototype.accept = function(module, action) {
filter = {'module': module, 'action': action}
this.filter.push(filter);
for(id in this.connections){
try{
this.connections[id].sendMessage('filter', 'accept', filter);
}
catch (e) {
//do nothing, the connection may have been removed, that's ok
}
}
}
Y.WSEvent.prototype.deny = function(module, action) {
filter = {'module': module, 'action': action}
this.filter.splice(this.filter.indexOf(filter), 1);
for(id in this.connections){
try{
this.connections[id].sendMessage('filter', 'deny', filter);
}
catch (e) {
//do nothing, the connection may have been removed, that's ok
}
}
}
Y.WSEvent.prototype.hasConnection = function(client) {
return client in this.connections && this.connections[client] != null;
}
Y.WSEvent.prototype.sendMessage = function(clientId, module, action, msg) {
if (clientId in this.connections && this.connections[clientId] != null) {
this.connections[clientId].sendMessage(module, action, msg);
return true;
} else {
return false;
}
}
}, '0.0.1', {requires: ['event', 'io', 'json-parse', 'json-stringify']});
id = ClientId
sockjsaddress = http://ebba.telldus.com:8001/ws/1
phpsess = ??? and here I come to my question. How do I get this phpsess, does it come from the Live authentication? A cookie somewhere?
Or is there another way of communicating back/getting notifications?
Thanks!