Skip to content

Commit

Permalink
Merge pull request #222 from tomvantilburg/master
Browse files Browse the repository at this point in the history
merge from Tom
  • Loading branch information
tomvantilburg authored Dec 9, 2016
2 parents badc2be + 7f83e72 commit 3508386
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 136 deletions.
7 changes: 4 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cow",
"version": "2.2.5",
"version": "2.3.0-beta",
"main": "dist/cow.js",
"homepage": "https://github.com/Geodan/cow",
"description": "Concurrent Online WebGIS",
Expand All @@ -27,7 +27,8 @@
"README.md"
],
"dependencies": {
"es6-promise": "^3.2.1",
"underscore": "~1"
"es6-promise": "3.2.1",
"underscore": "1.8.3",
"lzwCompress.js": "lzwcompress#0.2.4"
}
}
49 changes: 27 additions & 22 deletions dist/cow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2193,9 +2193,6 @@ Cow.websocket.prototype.disconnect = function() {
this._connection = null;
this._connected = false;
}
else {
console.log('No websocket active');
}
};

/**
Expand All @@ -2209,13 +2206,12 @@ Cow.websocket.prototype.connect = function() {
self._url = core.socketserver().url(); //get url from list of socketservers
}
else {
console.warn('No valid socketserver selected');
self._url = null;
reject('No valid socketserver selected');
}

if (!self._url) {
console.warn('Nu URL given to connect to. Make sure you give a valid socketserver id as connect(id)');
reject();
reject('No URL given to connect to. Make sure you give a valid socketserver id as connect(id)');
}

var connectpromise;
Expand All @@ -2234,8 +2230,7 @@ Cow.websocket.prototype.connect = function() {
self._connected = true;//TODO, perhaps better to check if the connection really works
}
else {
console.warn('Incorrect URL: ' + self._url);
reject();
reject('Incorrect URL: ' + self._url);
}
}
else {
Expand Down Expand Up @@ -2268,7 +2263,6 @@ Cow.websocket.prototype._onMessage = function(message){
Cow.websocket.prototype._onError = function(e){
this._core.peerStore().clear();
this._connected = false;
console.warn('error in websocket connection: ' + e.type);
this._core.websocket().trigger('error',e);
};

Expand All @@ -2278,22 +2272,23 @@ Cow.websocket.prototype._onClose = function(event){
var reason = event.reason;
var wasClean = event.wasClean;

console.log('WS disconnected:' , code, reason);
var notice = 'WS disconnected: ' + code + reason;
this._core.websocket().trigger('notice',notice);
this._core.peerStore().clear();
this._connected = false;
var self = this;
var restart = function(){
try{
console.log('Trying to reconnect');
this._core.websocket().trigger('notice','Trying to reconnect');
self._core.websocket().disconnect();
}
catch(err){
console.warn(err);
this._core.websocket().trigger('notice',err);
}
self._core.websocket().connect().then(function(d){
self._connection = d;
}, function(e){
console.warn('connection failed',e);
this._core.websocket().trigger('notice',e);
});
};
if (this._core._autoReconnect){
Expand Down Expand Up @@ -2446,7 +2441,8 @@ Cow.messenger.prototype.sendData = function(data, action, target){
message.sender = this._core.peerid();
message.target = target;
message.action = action;
message.payload = lzw_encode(encode_utf8(JSON.stringify(data)));
//message.payload = lzw_encode(encode_utf8(JSON.stringify(data)));
message.payload = lzwCompress.pack(data);
var stringified;
var endcoded;
try {
Expand All @@ -2460,22 +2456,32 @@ Cow.messenger.prototype.sendData = function(data, action, target){
this._amountsend = +stringified.length;
};

Cow.messenger.prototype._onError = function(error){
//TODO: propagate
};

Cow.messenger.prototype._onMessage = function(message){
var core = this._core;
var data = JSON.parse(message.data); //TODO: catch parse errors
var sender = data.sender;
var PEERID = core.peerid();
var action = data.action;
if (typeof(data.payload) == 'object'){
data.payload = data.payload;
//if (typeof(data.payload) == 'object'){
if (data.action == 'connected'){
data.payload = data.payload;
}
else {
data.payload = JSON.parse(decode_utf8(lzw_decode(data.payload)));
try {
//data.payload = JSON.parse(decode_utf8(lzw_decode(data.payload)));
data.payload = lzwCompress.unpack(data.payload);
}
catch(e){
this.trigger('notice','Error in lzwCompress ' + e);
}
}
var payload = data.payload;
var target = data.target;
if (sender != PEERID){
//console.info('Receiving '+JSON.stringify(data));
this._core.messenger()._numreqs++;
this._core.messenger()._amountreq = +message.data.length;
}
Expand Down Expand Up @@ -2558,7 +2564,6 @@ _onConnect handles 2 things
**/

Cow.messenger.prototype._onConnect = function(payload){
console.log('connected!');
this._connected = true;
var self = this;
this._core.peerid(payload.peerID);
Expand All @@ -2569,13 +2574,13 @@ Cow.messenger.prototype._onConnect = function(payload){
var now = new Date().getTime();
var maxdiff = 1000 * 60 * 5; //5 minutes
if (Math.abs(servertime - now) > maxdiff){
console.warn('Time difference between server and client larger ('+Math.abs(servertime-now)+'ms) than allowed ('+maxdiff+' ms).');
self.trigger('notice','Time difference between server and client larger ('+Math.abs(servertime-now)+'ms) than allowed ('+maxdiff+' ms).');
self.ws.disconnect();
return;
}

if (serverkey !== undefined && serverkey != this._core._herdname){
console.warn('Key on server ('+serverkey+') not the same as client key ('+this._core._herdname+').');
self.trigger('notice','Key on server ('+serverkey+') not the same as client key ('+this._core._herdname+').');
self.ws.disconnect();
return;
}
Expand Down Expand Up @@ -2911,7 +2916,7 @@ Cow.core = function(config){
if (typeof(config) == 'undefined' ) {
config = {};
}
this._version = '2.2.5';
this._version = '2.3.0-beta';
this._herdname = config.herdname || 'cow';
this._userid = null;
this._socketserverid = null;
Expand Down
6 changes: 3 additions & 3 deletions dist/cow.min.js

Large diffs are not rendered by default.

61 changes: 37 additions & 24 deletions dist/cow.nodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,6 @@ Cow.websocket.prototype.disconnect = function() {
this._connection.close();
this._connection = null;
}
else {
console.log('No websocket active');
}
};

/**
Expand All @@ -2046,11 +2043,13 @@ Cow.websocket.prototype.connect = function() {
if (core.socketserver()){
self._url = core.socketserver().url(); //get url from list of socketservers
}
else {
self._url = null;
reject('No valid socketserver selected');
}

if (!self._url) {
console.warn('Nu URL given to connect to. Make sure you give a valid socketserver id as connect(id)');
reject();
return false;
reject('No URL given to connect to. Make sure you give a valid socketserver id as connect(id)');
}

if (!self._connection || self._connection.readyState != 1 || self._connection.state != 'open') //if no connection
Expand All @@ -2059,11 +2058,13 @@ Cow.websocket.prototype.connect = function() {
var connection = null;
connection = new WebSocket();
connection.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
reject('Connect Error: ' + error.toString());
});
connection.on('connect', function(conn) {
console.log('WebSocket client connected');
conn.on('error', self._onError);
conn.on('close', function(){
core.websocket().trigger('notice','socket closed');
});
conn.on('message', function(message) {
if (message.type === 'utf8') {
//console.log("Received: '" + message.utf8Data + "'");
Expand All @@ -2072,21 +2073,23 @@ Cow.websocket.prototype.connect = function() {
});
conn.obj = self;
self._connection = conn;
resolve(self._connection);
});
//TODO: there is some issue with the websocket module,ssl and certificates
//This param should be added: {rejectUnauthorized: false}
//according to: http://stackoverflow.com/questions/18461979/node-js-error-with-ssl-unable-to-verify-leaf-signature#20408031
connection.connect(self._url, 'connect');

}
else {
console.warn('Incorrect URL: ' + self._url);
reject();
reject('Incorrect URL: ' + self._url);
}
}
else {
connection = self._connection;
resolve(self._connection);
}
recolve(connection);

});
return promise;
};
Expand All @@ -2110,11 +2113,11 @@ Cow.websocket.prototype._onMessage = function(message){
Cow.websocket.prototype._onError = function(e){
this._core.peerStore().clear();
this._connected = false;
console.warn('error in websocket connection: ' + e.type);
this._core.websocket().trigger('error');
this._core.websocket().trigger('error','error in websocket connection: ' + e.type);
};
Cow.websocket.prototype._onError = function(e){
console.log('closed');

Cow.websocket.prototype._onClose = function(event){
this.trigger('notice','socket closed');
};
_.extend(Cow.websocket.prototype, Events);
}.call(this));
Expand Down Expand Up @@ -2261,7 +2264,8 @@ Cow.messenger.prototype.sendData = function(data, action, target){
message.sender = this._core.peerid();
message.target = target;
message.action = action;
message.payload = lzw_encode(encode_utf8(JSON.stringify(data)));
//message.payload = lzw_encode(encode_utf8(JSON.stringify(data)));
message.payload = lzwCompress.pack(data);
var stringified;
var endcoded;
try {
Expand All @@ -2275,22 +2279,32 @@ Cow.messenger.prototype.sendData = function(data, action, target){
this._amountsend = +stringified.length;
};

Cow.messenger.prototype._onError = function(error){
//TODO: propagate
};

Cow.messenger.prototype._onMessage = function(message){
var core = this._core;
var data = JSON.parse(message.data); //TODO: catch parse errors
var sender = data.sender;
var PEERID = core.peerid();
var action = data.action;
if (typeof(data.payload) == 'object'){
data.payload = data.payload;
//if (typeof(data.payload) == 'object'){
if (data.action == 'connected'){
data.payload = data.payload;
}
else {
data.payload = JSON.parse(decode_utf8(lzw_decode(data.payload)));
try {
//data.payload = JSON.parse(decode_utf8(lzw_decode(data.payload)));
data.payload = lzwCompress.unpack(data.payload);
}
catch(e){
this.trigger('notice','Error in lzwCompress ' + e);
}
}
var payload = data.payload;
var target = data.target;
if (sender != PEERID){
//console.info('Receiving '+JSON.stringify(data));
this._core.messenger()._numreqs++;
this._core.messenger()._amountreq = +message.data.length;
}
Expand Down Expand Up @@ -2373,7 +2387,6 @@ _onConnect handles 2 things
**/

Cow.messenger.prototype._onConnect = function(payload){
console.log('connected!');
this._connected = true;
var self = this;
this._core.peerid(payload.peerID);
Expand All @@ -2384,13 +2397,13 @@ Cow.messenger.prototype._onConnect = function(payload){
var now = new Date().getTime();
var maxdiff = 1000 * 60 * 5; //5 minutes
if (Math.abs(servertime - now) > maxdiff){
console.warn('Time difference between server and client larger ('+Math.abs(servertime-now)+'ms) than allowed ('+maxdiff+' ms).');
self.trigger('notice','Time difference between server and client larger ('+Math.abs(servertime-now)+'ms) than allowed ('+maxdiff+' ms).');
self.ws.disconnect();
return;
}

if (serverkey !== undefined && serverkey != this._core._herdname){
console.warn('Key on server ('+serverkey+') not the same as client key ('+this._core._herdname+').');
self.trigger('notice','Key on server ('+serverkey+') not the same as client key ('+this._core._herdname+').');
self.ws.disconnect();
return;
}
Expand Down Expand Up @@ -2726,7 +2739,7 @@ Cow.core = function(config){
if (typeof(config) == 'undefined' ) {
config = {};
}
this._version = '2.2.5';
this._version = '2.3.0-beta';
this._herdname = config.herdname || 'cow';
this._userid = null;
this._socketserverid = null;
Expand Down
Loading

0 comments on commit 3508386

Please sign in to comment.