Skip to content

Commit

Permalink
Display server started notification on first client
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Jun 14, 2017
1 parent dc32e11 commit d427c92
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@
"MANDATORY_SECURITY_QUESTION": "Please select a security question.",
"MANDATORY_SECURITY_ANSWER": "Please provide an answer to your security question.",
"FORGOT_PASSWORD": "Forgot your password?",
"TITLE_FORGOT_PASSWORD": "Forgot Password"
"TITLE_FORGOT_PASSWORD": "Forgot Password",
"NOTIFICATION_SERVER_STARTED": "The server has been restarted! Your hacking progress has been restored automatically."
}
6 changes: 6 additions & 0 deletions app/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@

<h1 class="hidden" ng-bind="applicationName"></h1><!-- for SEO -->

<div class="container" ng-controller="ServerStartedNotificationController">
<div uib-alert ng-show="serverStartedMessage" ng-class="'alert alert-warning'" close="closeNotification()">
{{serverStartedMessage}}
</div>
</div>

<div class="container" ng-controller="ChallengeSolvedNotificationController">
<div uib-alert ng-repeat="notification in notifications" ng-class="'alert alert-success'" close="closeNotification($index)">
{{notification.message}}<br>
Expand Down
30 changes: 30 additions & 0 deletions app/js/controllers/ServerStartedNotificationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
angular.module('juiceShop').controller('ServerStartedNotificationController', [
'$scope',
'$translate',
'$cookies',
'ChallengeService',
'socket',
function ($scope, $translate, $cookies, challengeService, socket) {
'use strict'

$scope.closeNotification = function () {
$scope.serverStartedMessage = null
}

socket.on('server started', function () {
var continueCode = $cookies.get('continueCode')
console.log(continueCode)
if (continueCode) {
challengeService.restoreProgress(encodeURIComponent(continueCode)).then(function () {
$cookies.remove('continueCode')
$translate('NOTIFICATION_SERVER_STARTED').then(function (notificationServerStarted) {
$scope.serverStartedMessage = notificationServerStarted
}, function (translationId) {
$scope.serverStartedMessage = translationId
})
}).catch(function (error) {
console.log(error)
})
}
})
} ])
7 changes: 7 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var io = require('socket.io')(server)
var replace = require('replace')
var appConfiguration = require('./routes/appConfiguration')
const config = require('config')
var firstConnectedSocket = null

global.io = io
errorhandler.title = 'Juice Shop (Express ' + utils.version('express') + ')'
Expand Down Expand Up @@ -188,6 +189,12 @@ app.use(verify.errorHandlingChallenge())
app.use(errorhandler())

io.on('connection', function (socket) {
// notify only first client to connect about server start
if (firstConnectedSocket === null) {
socket.emit('server started')
firstConnectedSocket = socket.id
}

// send all outstanding notifications on (re)connect
notifications.forEach(function (notification) {
socket.emit('challenge solved', notification)
Expand Down

0 comments on commit d427c92

Please sign in to comment.