11'use strict'
2+ //Server for demo application
23
34const session = require ( 'express-session' )
45const express = require ( 'express' )
56const url = require ( 'url' )
67const qs = require ( 'qs' )
78const http = require ( 'http' )
89const WebSocket = require ( 'ws' )
9- //const staticGzip = require('static-gzip')
10- var compression = require ( 'compression' )
10+ const compression = require ( 'compression' )
1111
1212const app = express ( )
1313
@@ -19,17 +19,22 @@ const sessionHandler = session({
1919
2020app . use ( compression ( ) )
2121app . use ( sessionHandler )
22+ app . use ( express . static ( 'dist' ) )
2223
23- const server = http . createServer ( app ) ;
24+ const server = http . createServer ( app )
2425
2526const wss = new WebSocket . Server ( {
2627 server : server ,
2728 path : '/websocket'
2829} )
2930
30- app . use ( express . static ( 'dist' ) )
31+ server . listen ( 3000 , ( ) => {
32+ console . log ( 'Listening on %d' , server . address ( ) . port )
33+ } ) ;
3134
32- app . get ( '/' , function ( req , res , next ) {
35+ //Return the starting HTML to the client
36+ //Just inlined it here as this is just an example server.
37+ app . get ( '/' , ( req , res , next ) => {
3338 const response = `
3439 <!DOCTYPE html>
3540 <html lang="en">
@@ -45,6 +50,7 @@ app.get('/', function(req, res, next){
4550 <div class="page-header">
4651 <h1>Todo Example</h1>
4752 </div>
53+ <div id="count">${ renderTodoCount ( req . session ) } </div>
4854 <ul id="lists" class="list-group">
4955 ${ renderTodos ( req . session ) }
5056 </ul>
@@ -66,57 +72,55 @@ app.get('/', function(req, res, next){
6672 res . send ( response )
6773} )
6874
69- wss . on ( 'connection' , function ( ws ) {
70- let req = {
75+ wss . on ( 'connection' , ( ws ) => {
76+ const req = { //Fake a request that we would normally see over HTTP
7177 originalUrl : '/' ,
7278 pathname : '/' ,
7379 headers : ws . upgradeReq . headers
7480 }
75- sessionHandler ( req , { } , function ( ) {
76- ws . on ( 'message' , function ( msg ) {
81+ sessionHandler ( req , { } , ( ) => { //Process session once a client connects
82+ ws . on ( 'message' , ( msg ) => {
7783 console . log ( msg )
7884 const parsed = url . parse ( msg )
79- processMessage ( ws , parsed , req . session )
85+ routeMessage ( ws , parsed , req . session )
8086 } )
81-
8287 } )
8388} )
8489
85- server . listen ( 3000 , function listening ( ) {
86- console . log ( 'Listening on %d' , server . address ( ) . port ) ;
87- } ) ;
88-
89-
90- const processMessage = function ( ws , request , session ) {
90+ //Simple router
91+ const routeMessage = ( ws , request , session ) => {
9192 const query = qs . parse ( request . query )
93+ //The example app only has these functions
9294 if ( request . pathname === '/add' ) {
93- addRoute ( query , session , function ( out ) {
95+ addRoute ( query , session , ( out ) => {
9496 ws . send ( JSON . stringify ( out ) )
9597 } )
9698 }
9799 else if ( request . pathname === '/delete' ) {
98- deleteRoute ( query , session , function ( out ) {
100+ deleteRoute ( query , session , ( out ) => {
99101 ws . send ( JSON . stringify ( out ) )
100102 } )
101103 }
102- } ;
104+ }
103105
104- const deleteRoute = function ( query , session , cb ) {
105- const filtered = session . todos . filter ( function ( item ) {
106+ //Deleting a todo
107+ const deleteRoute = ( query , session , cb ) => {
108+ const filtered = session . todos . filter ( ( item ) => {
106109 return item . id !== parseInt ( query . id , 10 )
107110 } )
108111 session . todos = filtered
109- session . save ( function ( ) {
112+ session . save ( ( ) => {
110113 const response = [ {
111114 action : 'replace' ,
112115 container : 'lists' ,
113116 content : renderTodos ( session )
114117 } ]
115118 cb ( response )
116119 } )
117- } ;
120+ }
118121
119- const addRoute = function ( query , session , cb ) {
122+ //Adding a todo
123+ const addRoute = ( query , session , cb ) => {
120124 if ( ! session . todos ) {
121125 session . todos = [ ]
122126 }
@@ -126,37 +130,50 @@ const addRoute = function(query, session, cb){
126130 reminder : query . reminder
127131 }
128132 session . todos . push ( todo )
129- session . save ( function ( ) {
133+ session . save ( ( ) => {
130134 const response = [ {
131135 action : 'append' ,
132136 container : 'lists' ,
133137 content : '<li class="list-group-item">' + todo . description + ' <button class="pull-right" data-url="/delete?id=' + todo . id + '">Delete</button></li>'
138+ } , {
139+ action : 'replace' ,
140+ container : 'count' ,
141+ content : renderTodoCount ( session )
134142 } ]
135143 if ( todo . reminder !== '0' ) {
136144 console . log ( 'scheduling reminder' )
137145 sendReminder ( todo , cb )
138146 }
139147 cb ( response )
140148 } )
141- } ;
149+ }
142150
143- const sendReminder = function ( todo , cb ) {
144- setTimeout ( function ( ) {
151+ //Send a message back to the client at some point in the future.
152+ //cb() is the send function on the websocket so we can call it as many times as we need.
153+ const sendReminder = ( todo , cb ) => {
154+ setTimeout ( ( ) => {
145155 const reminderResponse = [ {
146156 action : 'replace' ,
147157 container : 'reminder' ,
148158 content : '<div class="alert alert-info" role="alert" onclick="this.style.display = \'none\';">Reminder for ' + todo . description + '!</div>'
149159 } ]
150160 cb ( reminderResponse )
151161 } , todo . reminder * 1000 )
152- } ;
162+ }
153163
154- const renderTodos = function ( sess ) {
164+ //Generate the HTML for all the todos
165+ const renderTodos = ( sess ) => {
155166 if ( ! sess . todos ) {
156167 return ''
157168 }
158- return sess . todos . reduce ( function ( acc , todo ) {
169+ return sess . todos . reduce ( ( acc , todo ) => {
159170 acc += '<li class="list-group-item">' + todo . description + ' <button class="pull-right" data-url="/delete?id=' + todo . id + '">Delete</button></li>'
160171 return acc
161172 } , '' )
162173}
174+
175+ //Generate the HTML for the number of todos
176+ const renderTodoCount = ( sess ) => {
177+ const number = sess . todos ? sess . todos . length : 0
178+ return `You have <strong>${ number } </strong> todos right now.`
179+ }
0 commit comments