Add Current Visitors count display
This commit is contained in:
		
							parent
							
								
									1078e6f6e5
								
							
						
					
					
						commit
						7c5faf7e49
					
				
					 3 changed files with 18 additions and 11 deletions
				
			
		| 
						 | 
					@ -3,6 +3,10 @@ $(document).ready(function() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  var downloadButton;
 | 
					  var downloadButton;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  socket.on('update visitors', function(visitors) {
 | 
				
			||||||
 | 
					    $('#visitors').text(visitors);
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  socket.on('get book', function(url) {
 | 
					  socket.on('get book', function(url) {
 | 
				
			||||||
    $(downloadButton).replaceWith('<a download href="' + url + '" class="button is-success is-large">Download</a>');
 | 
					    $(downloadButton).replaceWith('<a download href="' + url + '" class="button is-success is-large">Download</a>');
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										13
									
								
								server.js
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								server.js
									
										
									
									
									
								
							| 
						 | 
					@ -24,6 +24,7 @@ function Server () {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  this.templateCache = {};
 | 
					  this.templateCache = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  this.connections = 0;
 | 
				
			||||||
  this.takenBooks = [];
 | 
					  this.takenBooks = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  this.server.use(helmet());
 | 
					  this.server.use(helmet());
 | 
				
			||||||
| 
						 | 
					@ -136,7 +137,8 @@ function Server () {
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  this.io.on('connection', socket => {
 | 
					  this.io.on('connection', socket => {
 | 
				
			||||||
    this.broadcastVisitors();
 | 
					    this.connections++;
 | 
				
			||||||
 | 
					    this.io.emit('update visitors', this.connections);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    socket.on('take book', bookId => {
 | 
					    socket.on('take book', bookId => {
 | 
				
			||||||
      const fileLocation = this.takeBook(bookId, socket.id);
 | 
					      const fileLocation = this.takeBook(bookId, socket.id);
 | 
				
			||||||
| 
						 | 
					@ -149,8 +151,8 @@ function Server () {
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    socket.on('disconnect', () => {
 | 
					    socket.on('disconnect', () => {
 | 
				
			||||||
      // console.log(socket.id + ' disconnected');
 | 
					      this.connections--;
 | 
				
			||||||
      this.broadcastVisitors();
 | 
					      this.io.emit('update visitors', this.connections);
 | 
				
			||||||
      this.deleteBooks(socket.id);
 | 
					      this.deleteBooks(socket.id);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
| 
						 | 
					@ -276,11 +278,6 @@ Server.prototype.generateHistoryPage = function (req) {
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Server.prototype.broadcastVisitors = function () {
 | 
					 | 
				
			||||||
  const numberConnected = this.io.of('/').clients().connected.length;
 | 
					 | 
				
			||||||
  this.io.emit('connected', numberConnected);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Server.prototype.start = function () {
 | 
					Server.prototype.start = function () {
 | 
				
			||||||
  this.http.listen(settings.port, () => {
 | 
					  this.http.listen(settings.port, () => {
 | 
				
			||||||
    console.log('Started server on port ' + settings.port);
 | 
					    console.log('Started server on port ' + settings.port);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,13 @@
 | 
				
			||||||
  <nav class="navbar" role="navigation" aria-label="main navigation">
 | 
					  <nav class="navbar" role="navigation" aria-label="main navigation">
 | 
				
			||||||
    <div class="navbar-brand">
 | 
					    <div class="navbar-brand">
 | 
				
			||||||
      <a class="navbar-item" href="/">
 | 
					      <a class="navbar-item" href="/">
 | 
				
			||||||
 | 
					        <div>
 | 
				
			||||||
          <h1 class="title">{{siteTitle}}</h1>
 | 
					          <h1 class="title">{{siteTitle}}</h1>
 | 
				
			||||||
 | 
					          <div class="subtitle tags has-addons">
 | 
				
			||||||
 | 
					            <span class="tag">Current Visitors</span>
 | 
				
			||||||
 | 
					            <span class="tag is-primary" id="visitors">…</span>
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
      </a>
 | 
					      </a>
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
      <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false">
 | 
					      <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false">
 | 
				
			||||||
| 
						 | 
					@ -66,8 +72,8 @@
 | 
				
			||||||
  <footer class="footer">
 | 
					  <footer class="footer">
 | 
				
			||||||
    <div class="content has-text-centered">
 | 
					    <div class="content has-text-centered">
 | 
				
			||||||
      <p>
 | 
					      <p>
 | 
				
			||||||
        <strong>Little Library</strong> by <a href="https://robbie.antenesse.net">Robbie Antenesse</a>. The source code is licensed
 | 
					        <strong>Little Library</strong> by <a href="https://robbie.antenesse.net">Robbie Antenesse</a>.
 | 
				
			||||||
        <a href="http://opensource.org/licenses/mit-license.php">MIT</a>. The website content
 | 
					        The source code is licensed <a href="http://opensource.org/licenses/mit-license.php">MIT</a>. The website content
 | 
				
			||||||
        is licensed <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY NC SA 4.0</a>.
 | 
					        is licensed <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY NC SA 4.0</a>.
 | 
				
			||||||
      </p>
 | 
					      </p>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue