Skip to content

Commit

Permalink
Merge pull request #32 from euforic/TIMOB-13931
Browse files Browse the repository at this point in the history
[TIMOB-13931] add proper removal of closed sockets on server and reset context on client
  • Loading branch information
Christian Sullivan committed May 25, 2013
2 parents 7af9325 + 75b4736 commit d1b33f3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 29 deletions.
47 changes: 33 additions & 14 deletions build/liveview.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ Socket.prototype.close = function(serverEnded){
self.closing = !serverEnded;

if (self.closing){
self._proxy.close();
return self.emit('close');
self.write(function(){
self._proxy.close();
self.emit('close');
});
return
}

var retry = ~~self.retry;
Expand All @@ -260,11 +263,13 @@ Socket.prototype.write = function(data, fn) {
data = null;
}

data = (data) ? ('' + data) : '';

var msg = Ti.createBuffer({value: data});

var callback = fn || function(){};

Ti.Stream.write(this._connection, Ti.createBuffer({
value: '' + data
}), function(){
Ti.Stream.write(this._connection, msg, function(){
callback([].slice(arguments));
});

Expand Down Expand Up @@ -386,27 +391,41 @@ Module.patch = function (globalCtx, port, url) {
Module._url = url || 'FSERVER_HOST';
Module._port = port || 8324;
Module._requireNative = globalCtx.require;
Module.evtServer && Module.evtServer.close();
Module.connectServer();
Module.require('app');
}

/**
/**
* [reload description]
* @return {[type]} [description]
*/

this.global.reload = function(){
Module.global.reload = function(){
try {
Module.evtServer.close();
Module = null;
console.log('[LiveView] Reloading App');
try {
Ti.App._restart();
} catch(e){
require('app');
}
};
Ti.App._restart();
} catch(e){
console.log('[LiveView] Reloading App via Legacy Method');
Module.require('app');
}
};


Module.connectServer = function() {

var retryInterval = null;

var client = this.evtServer = new Socket({host: 'TCP_HOST', port: 8323}, function() {
var client = Module.evtServer = new Socket({host: 'TCP_HOST', port: 8323}, function() {
console.log('[LiveView]', 'Connected to Event Server');
});

client.on('close', function(){
console.log('[LiveView]', 'Closed Previous Event Server client');
});

client.on('connect', function(){
if (retryInterval) {
clearInterval(retryInterval);
Expand Down
2 changes: 1 addition & 1 deletion build/liveview.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions lib/fserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,29 @@ FServer.start = function(opts) {
evtServer.on('connection', function (client) {
console.log('[LiveView]'.green, 'Client connected');
evtServer.clients.push(client);

client.on('end', function(e){
console.log('[LiveView]'.green, 'Client disconnected');
evtServer.clients.splice(client);
})

client.on('data', function(e){
console.log('data:'+e);
})
});

evtServer.on('close', function (client) {
var clients = evtServer.clients;
var clientLen = clients.length;

for (var i = 0; i < clientLen; i++) {
console.log(client[i]);
//clients.splice(i);
}
});



evtServer.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
Expand Down
32 changes: 23 additions & 9 deletions lib/platform/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,41 @@ Module.patch = function (globalCtx, port, url) {
Module._url = url || 'FSERVER_HOST';
Module._port = port || 8324;
Module._requireNative = globalCtx.require;
Module.evtServer && Module.evtServer.close();
Module.connectServer();
Module.require('app');
}

/**
/**
* [reload description]
* @return {[type]} [description]
*/

this.global.reload = function(){
Module.global.reload = function(){
try {
Module.evtServer.close();
Module = null;
console.log('[LiveView] Reloading App');
try {
Ti.App._restart();
} catch(e){
require('app');
}
};
Ti.App._restart();
} catch(e){
console.log('[LiveView] Reloading App via Legacy Method');
Module.require('app');
}
};


Module.connectServer = function() {

var retryInterval = null;

var client = this.evtServer = new Socket({host: 'TCP_HOST', port: 8323}, function() {
var client = Module.evtServer = new Socket({host: 'TCP_HOST', port: 8323}, function() {
console.log('[LiveView]', 'Connected to Event Server');
});

client.on('close', function(){
console.log('[LiveView]', 'Closed Previous Event Server client');
});

client.on('connect', function(){
if (retryInterval) {
clearInterval(retryInterval);
Expand Down
15 changes: 10 additions & 5 deletions lib/platform/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ Socket.prototype.close = function(serverEnded){
self.closing = !serverEnded;

if (self.closing){
self._proxy.close();
return self.emit('close');
self.write(function(){
self._proxy.close();
self.emit('close');
});
return
}

var retry = ~~self.retry;
Expand All @@ -111,11 +114,13 @@ Socket.prototype.write = function(data, fn) {
data = null;
}

data = (data) ? ('' + data) : '';

var msg = Ti.createBuffer({value: data});

var callback = fn || function(){};

Ti.Stream.write(this._connection, Ti.createBuffer({
value: '' + data
}), function(){
Ti.Stream.write(this._connection, msg, function(){
callback([].slice(arguments));
});

Expand Down

0 comments on commit d1b33f3

Please sign in to comment.