Skip to content

Commit a1fe1c1

Browse files
committed
Refactoring of Websocket
1 parent a36adf9 commit a1fe1c1

File tree

11 files changed

+160
-148
lines changed

11 files changed

+160
-148
lines changed

zeppelin-web/bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"angular-touch": "1.3.8",
1313
"angular-route": "1.3.8",
1414
"angular-bootstrap": "~0.11.0",
15-
"angular-websocket": "~0.0.5",
15+
"angular-websocket": "~1.0.13",
1616
"ace-builds": "1.1.8",
1717
"angular-ui-ace": "0.1.1",
1818
"jquery.scrollTo": "~1.4.13",

zeppelin-web/src/app/app.controller.js

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,82 +22,16 @@
2222
*
2323
* @author anthonycorbacho
2424
*/
25-
angular.module('zeppelinWebApp').controller('MainCtrl', function($scope, WebSocket, $rootScope, $window) {
25+
angular.module('zeppelinWebApp').controller('MainCtrl', function($scope, $rootScope, $window) {
2626
$rootScope.compiledScope = $scope.$new(true, $rootScope);
27-
$scope.WebSocketWaitingList = [];
28-
$scope.connected = false;
2927
$scope.looknfeel = 'default';
3028

3129
var init = function() {
3230
$scope.asIframe = (($window.location.href.indexOf('asIframe') > -1) ? true : false);
3331
};
32+
3433
init();
3534

36-
/**
37-
* Web socket
38-
*/
39-
WebSocket.onopen(function() {
40-
console.log('Websocket created');
41-
$scope.connected = true;
42-
if ($scope.WebSocketWaitingList.length > 0) {
43-
for (var o in $scope.WebSocketWaitingList) {
44-
WebSocket.send(JSON.stringify($scope.WebSocketWaitingList[o]));
45-
}
46-
}
47-
});
48-
49-
WebSocket.onmessage(function(event) {
50-
var payload;
51-
if (event.data) {
52-
payload = angular.fromJson(event.data);
53-
}
54-
console.log('Receive << %o, %o, %o', payload.op, payload, $scope);
55-
var op = payload.op;
56-
var data = payload.data;
57-
if (op === 'NOTE') {
58-
$rootScope.$broadcast('setNoteContent', data.note);
59-
} else if (op === 'NOTES_INFO') {
60-
$rootScope.$broadcast('setNoteMenu', data.notes);
61-
} else if (op === 'PARAGRAPH') {
62-
$scope.$broadcast('updateParagraph', data);
63-
} else if (op === 'PROGRESS') {
64-
$rootScope.$broadcast('updateProgress', data);
65-
} else if (op === 'COMPLETION_LIST') {
66-
$rootScope.$broadcast('completionList', data);
67-
} else if (op === 'ANGULAR_OBJECT_UPDATE') {
68-
$rootScope.$broadcast('angularObjectUpdate', data);
69-
}
70-
});
71-
72-
WebSocket.onerror(function(event) {
73-
console.log('error message: ', event);
74-
$scope.connected = false;
75-
});
76-
77-
WebSocket.onclose(function(event) {
78-
console.log('close message: ', event);
79-
$scope.connected = false;
80-
});
81-
82-
/** Send info to the websocket server */
83-
var send = function(data) {
84-
if (WebSocket.currentState() !== 'OPEN') {
85-
$scope.WebSocketWaitingList.push(data);
86-
} else {
87-
console.log('Send >> %o, %o', data.op, data);
88-
WebSocket.send(JSON.stringify(data));
89-
}
90-
};
91-
92-
93-
/** get the childs event and sebd to the websocket server */
94-
$rootScope.$on('sendNewEvent', function(event, data) {
95-
if (!event.defaultPrevented) {
96-
send(data);
97-
event.preventDefault();
98-
}
99-
});
100-
10135
$rootScope.$on('setIframe', function(event, data) {
10236
if (!event.defaultPrevented) {
10337
$scope.asIframe = data;

zeppelin-web/src/app/app.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,50 +36,7 @@
3636
* In the case of running "grunt serve", this function will appear
3737
* as is.
3838
*/
39-
function getPort() {
40-
var port = Number(location.port);
41-
if (location.protocol !== 'https:' && (port === 'undifined' || port === 0))
42-
port = 80;
43-
else if (location.protocol === 'https:' && (port === 'undifined' || port === 0))
44-
port = 443;
45-
else if (port === 3333 || port === 9000)
46-
port = 8080;
47-
return port+1;
48-
}
4939

50-
function getWebsocketProtocol() {
51-
var protocol = 'ws';
52-
if (location.protocol === 'https:') {
53-
protocol = 'wss';
54-
}
55-
return protocol;
56-
}
57-
58-
function getRestApiBase() {
59-
var port = Number(location.port);
60-
if (port === 'undefined' || port === 0) {
61-
port = 80;
62-
if (location.protocol === 'https:') {
63-
port = 443;
64-
}
65-
}
66-
67-
if (port === 3333 || port === 9000) {
68-
port = 8080;
69-
}
70-
return location.protocol + '//' + location.hostname + ':' + port + '/api';
71-
}
72-
73-
/**
74-
* @ngdoc overview
75-
* @name zeppelinWebApp
76-
* @description
77-
* # zeppelinWebApp
78-
*
79-
* Main module of the application.
80-
*
81-
* @author anthonycorbacho
82-
*/
8340
angular
8441
.module('zeppelinWebApp', [
8542
'ngAnimate',
@@ -103,10 +60,7 @@ angular
10360
}
10461
};
10562
})
106-
.config(function ($routeProvider, WebSocketProvider) {
107-
WebSocketProvider
108-
.prefix('')
109-
.uri(getWebsocketProtocol() + '://' + location.hostname + ':' + getPort());
63+
.config(function ($routeProvider) {
11064

11165
$routeProvider
11266
.when('/', {
@@ -129,6 +83,3 @@ angular
12983
redirectTo: '/'
13084
});
13185
});
132-
133-
134-

zeppelin-web/src/app/interpreter/interpreter.controller.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global confirm:false, alert:false, getRestApiBase:false */
1+
/* global confirm:false, alert:false */
22
/* jshint loopfunc: true */
33
/*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,7 +22,8 @@
2222
* # InterpreterCtrl
2323
* Controller of interpreter, manage the note (update)
2424
*/
25-
angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope, $route, $routeParams, $location, $rootScope, $http) {
25+
angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope, $route, $routeParams, $location, $rootScope,
26+
$http, baseUrlSrv) {
2627

2728
var remoteSettingToLocalSetting = function(settingId, setting) {
2829
var property = {};
@@ -42,7 +43,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
4243
};
4344

4445
var getInterpreterSettings = function() {
45-
$http.get(getRestApiBase()+'/interpreter/setting').
46+
$http.get(baseUrlSrv.getRestApiBase()+'/interpreter/setting').
4647
success(function(data, status, headers, config) {
4748
var interpreterSettings = [];
4849
//console.log("getInterpreterSettings=%o", data);
@@ -59,7 +60,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
5960
};
6061

6162
var getAvailableInterpreters = function() {
62-
$http.get(getRestApiBase()+'/interpreter').
63+
$http.get(baseUrlSrv.getRestApiBase()+'/interpreter').
6364
success(function(data, status, headers, config) {
6465
var groupedInfo = {};
6566
var info;
@@ -122,7 +123,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
122123
}
123124
}
124125

125-
$http.put(getRestApiBase()+'/interpreter/setting/'+settingId, request).
126+
$http.put(baseUrlSrv.getRestApiBase()+'/interpreter/setting/'+settingId, request).
126127
success(function(data, status, headers, config) {
127128
for (var i=0; i < $scope.interpreterSettings.length; i++) {
128129
var setting = $scope.interpreterSettings[i];
@@ -156,7 +157,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
156157
}
157158

158159
console.log('Delete setting %o', settingId);
159-
$http.delete(getRestApiBase()+'/interpreter/setting/'+settingId).
160+
$http.delete(baseUrlSrv.getRestApiBase()+'/interpreter/setting/'+settingId).
160161
success(function(data, status, headers, config) {
161162
for (var i=0; i < $scope.interpreterSettings.length; i++) {
162163
var setting = $scope.interpreterSettings[i];
@@ -192,7 +193,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
192193
return;
193194
}
194195

195-
$http.put(getRestApiBase()+'/interpreter/setting/restart/'+settingId).
196+
$http.put(baseUrlSrv.getRestApiBase()+'/interpreter/setting/restart/'+settingId).
196197
success(function(data, status, headers, config) {
197198
for (var i=0; i < $scope.interpreterSettings.length; i++) {
198199
var setting = $scope.interpreterSettings[i];
@@ -235,7 +236,7 @@ angular.module('zeppelinWebApp').controller('InterpreterCtrl', function($scope,
235236
newSetting.properties[p] = $scope.newInterpreterSetting.properties[p].value;
236237
}
237238

238-
$http.post(getRestApiBase()+'/interpreter/setting', newSetting).
239+
$http.post(baseUrlSrv.getRestApiBase()+'/interpreter/setting', newSetting).
239240
success(function(data, status, headers, config) {
240241
$scope.resetNewInterpreterSetting();
241242
getInterpreterSettings();

zeppelin-web/src/app/notebook/notebook.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Controller of notes, manage the note (update)
2424
*
2525
*/
26-
angular.module('zeppelinWebApp').controller('NotebookCtrl', function($scope, $route, $routeParams, $location, $rootScope, $http, websocketMsgSrv) {
26+
angular.module('zeppelinWebApp').controller('NotebookCtrl', function($scope, $route, $routeParams, $location, $rootScope, $http, websocketMsgSrv, baseUrlSrv) {
2727
$scope.note = null;
2828
$scope.showEditor = false;
2929
$scope.editorToggled = false;
@@ -351,7 +351,7 @@ angular.module('zeppelinWebApp').controller('NotebookCtrl', function($scope, $ro
351351
};
352352

353353
var getInterpreterBindings = function(callback) {
354-
$http.get(getRestApiBase()+ '/notebook/interpreter/bind/' +$scope.note.id).
354+
$http.get(baseUrlSrv.getRestApiBase()+ '/notebook/interpreter/bind/' +$scope.note.id).
355355
success(function(data, status, headers, config) {
356356
$scope.interpreterBindings = data.body;
357357
$scope.interpreterBindingsOrig = jQuery.extend(true, [], $scope.interpreterBindings); // to check dirty
@@ -418,7 +418,7 @@ angular.module('zeppelinWebApp').controller('NotebookCtrl', function($scope, $ro
418418
}
419419
}
420420

421-
$http.put(getRestApiBase() + '/notebook/interpreter/bind/' + $scope.note.id,
421+
$http.put(baseUrlSrv.getRestApiBase() + '/notebook/interpreter/bind/' + $scope.note.id,
422422
selectedSettingIds).
423423
success(function(data, status, headers, config) {
424424
console.log('Interpreter binding %o saved', selectedSettingIds);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
'use strict';
15+
16+
angular.module('zeppelinWebApp').service('baseUrlSrv', function() {
17+
18+
this.getPort = function() {
19+
var port = Number(location.port);
20+
if (location.protocol !== 'https:' && (port === 'undifined' || port === 0)) {
21+
port = 80;
22+
} else if (location.protocol === 'https:' && (port === 'undifined' || port === 0)) {
23+
port = 443;
24+
} else if (port === 3333 || port === 9000) {
25+
port = 8080;
26+
}
27+
return port+1;
28+
};
29+
30+
this.getWebsocketProtocol = function() {
31+
var protocol = 'ws';
32+
if (location.protocol === 'https:') {
33+
protocol = 'wss';
34+
}
35+
return protocol;
36+
};
37+
38+
this.getRestApiBase = function() {
39+
var port = Number(location.port);
40+
if (port === 'undefined' || port === 0) {
41+
port = 80;
42+
if (location.protocol === 'https:') {
43+
port = 443;
44+
}
45+
}
46+
47+
if (port === 3333 || port === 9000) {
48+
port = 8080;
49+
}
50+
return location.protocol + '//' + location.hostname + ':' + port + '/api';
51+
};
52+
53+
});

zeppelin-web/src/components/navbar/navbar.controller.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ angular.module('zeppelinWebApp').controller('NavCtrl', function($scope, $rootSco
3030

3131
var vm = this;
3232
vm.notes = notebookListDataFactory;
33+
vm.connected = false;
3334
vm.websocketMsgSrv = websocketMsgSrv;
3435

3536
$('#notebook-list').perfectScrollbar({suppressScrollX: true});
3637

3738
$scope.$on('setNoteMenu', function(event, notes) {
3839
notebookListDataFactory.setNotes(notes);
3940
});
41+
42+
$scope.$on('setConnectedStatus', function(event, param) {
43+
vm.connected = param;
44+
});
4045

4146
function loadNotes() {
4247
websocketMsgSrv.getNotebookList();

zeppelin-web/src/components/navbar/navbar.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
</ul>
3232
<ul class="nav navbar-nav navbar-right" style="margin-top:10px; margin-right:5px;">
3333
<li class="server-status">
34-
<i class="fa fa-circle" ng-class="{'server-connected':connected, 'server-disconnected':!connected }"></i>
35-
<span ng-show="connected">Connected</span>
36-
<span ng-show="!connected">Disconnected</span>
34+
<i class="fa fa-circle" ng-class="{'server-connected':navbar.connected, 'server-disconnected':!navbar.connected}"></i>
35+
<span ng-show="navbar.connected">Connected</span>
36+
<span ng-show="!navbar.connected">Disconnected</span>
3737
</li>
3838
</ul>
3939
</div>

0 commit comments

Comments
 (0)