mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	websocket reimplementation of status requests
This commit is contained in:
		
							parent
							
								
									f433b30089
								
							
						
					
					
						commit
						992238f0b3
					
				
							
								
								
									
										17
									
								
								bin/www
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								bin/www
									
									
									
									
									
								
							| @ -16,6 +16,7 @@ const https = require('https'); | |||||||
| const config = require('../services/config'); | const config = require('../services/config'); | ||||||
| const log = require('../services/log'); | const log = require('../services/log'); | ||||||
| const app_info = require('../services/app_info'); | const app_info = require('../services/app_info'); | ||||||
|  | const messaging = require('../services/messaging'); | ||||||
| 
 | 
 | ||||||
| const port = normalizePort(config['Network']['port'] || '3000'); | const port = normalizePort(config['Network']['port'] || '3000'); | ||||||
| app.set('port', port); | app.set('port', port); | ||||||
| @ -23,7 +24,7 @@ app.set('port', port); | |||||||
| /** | /** | ||||||
|  * Create HTTP server. |  * Create HTTP server. | ||||||
|  */ |  */ | ||||||
| let server; | let httpServer; | ||||||
| 
 | 
 | ||||||
| if (config['Network']['https']) { | if (config['Network']['https']) { | ||||||
|     const options = { |     const options = { | ||||||
| @ -31,12 +32,12 @@ if (config['Network']['https']) { | |||||||
|         cert: fs.readFileSync(config['Network']['certPath']) |         cert: fs.readFileSync(config['Network']['certPath']) | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     server = https.createServer(options, app); |     httpServer = https.createServer(options, app); | ||||||
| 
 | 
 | ||||||
|     log.info("App HTTPS server starting up at port " + port); |     log.info("App HTTPS server starting up at port " + port); | ||||||
| } | } | ||||||
| else { | else { | ||||||
|     server = http.createServer(app); |     httpServer = http.createServer(app); | ||||||
| 
 | 
 | ||||||
|     log.info("App HTTP server starting up at port " + port); |     log.info("App HTTP server starting up at port " + port); | ||||||
| } | } | ||||||
| @ -47,9 +48,11 @@ log.info(JSON.stringify(app_info, null, 2)); | |||||||
|  * Listen on provided port, on all network interfaces. |  * Listen on provided port, on all network interfaces. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| server.listen(port); | httpServer.listen(port); | ||||||
| server.on('error', onError); | httpServer.on('error', onError); | ||||||
| server.on('listening', onListening); | httpServer.on('listening', onListening); | ||||||
|  | 
 | ||||||
|  | messaging.init(httpServer); | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Normalize a port into a number, string, or false. |  * Normalize a port into a number, string, or false. | ||||||
| @ -106,7 +109,7 @@ function onError(error) { | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| function onListening() { | function onListening() { | ||||||
|     const addr = server.address(); |     const addr = httpServer.address(); | ||||||
|     const bind = typeof addr === 'string' |     const bind = typeof addr === 'string' | ||||||
|         ? 'pipe ' + addr |         ? 'pipe ' + addr | ||||||
|         : 'port ' + addr.port; |         : 'port ' + addr.port; | ||||||
|  | |||||||
| @ -17,6 +17,7 @@ | |||||||
|     "body-parser": "~1.18.2", |     "body-parser": "~1.18.2", | ||||||
|     "cookie-parser": "~1.4.3", |     "cookie-parser": "~1.4.3", | ||||||
|     "debug": "~3.1.0", |     "debug": "~3.1.0", | ||||||
|  |     "devtron": "^1.4.0", | ||||||
|     "ejs": "~2.5.7", |     "ejs": "~2.5.7", | ||||||
|     "electron": "^1.8.2-beta.2", |     "electron": "^1.8.2-beta.2", | ||||||
|     "electron-debug": "^1.0.0", |     "electron-debug": "^1.0.0", | ||||||
| @ -32,7 +33,7 @@ | |||||||
|     "session-file-store": "^1.1.2", |     "session-file-store": "^1.1.2", | ||||||
|     "simple-node-logger": "^0.93.30", |     "simple-node-logger": "^0.93.30", | ||||||
|     "sqlite": "^2.8.0", |     "sqlite": "^2.8.0", | ||||||
|     "devtron": "^1.4.0" |     "ws": "^3.3.2" | ||||||
|   }, |   }, | ||||||
|   "devDependencies": { |   "devDependencies": { | ||||||
|     "electron-compile": "^6.4.2", |     "electron-compile": "^6.4.2", | ||||||
|  | |||||||
| @ -140,3 +140,30 @@ function initAjax() { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| initAjax(); | initAjax(); | ||||||
|  | 
 | ||||||
|  | // use wss for secure messaging
 | ||||||
|  | const ws = new WebSocket("ws://" + location.host); | ||||||
|  | ws.onopen = function (event) { | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | ws.onmessage = function (event) { | ||||||
|  |     console.log(event.data); | ||||||
|  | 
 | ||||||
|  |     const message = JSON.parse(event.data); | ||||||
|  | 
 | ||||||
|  |     if (message.type === 'sync') { | ||||||
|  |         const data = message.data; | ||||||
|  | 
 | ||||||
|  |         if (data.notes_tree) { | ||||||
|  |             console.log("Reloading tree because of background changes"); | ||||||
|  | 
 | ||||||
|  |             noteTree.reload(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) { | ||||||
|  |             showMessage('Reloading note because background change'); | ||||||
|  | 
 | ||||||
|  |             noteEditor.reload(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }; | ||||||
| @ -26,17 +26,17 @@ const status = (function() { | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         if (resp.changedTree) { |         // if (resp.changedTree) {
 | ||||||
|             console.log("Reloading tree because of background changes"); |         //     console.log("Reloading tree because of background changes");
 | ||||||
| 
 |         //
 | ||||||
|             noteTree.reload(); |         //     noteTree.reload();
 | ||||||
|         } |         // }
 | ||||||
| 
 |         //
 | ||||||
|         if (resp.changedCurrentNote) { |         // if (resp.changedCurrentNote) {
 | ||||||
|             showMessage('Reloading note because background change'); |         //     showMessage('Reloading note because background change');
 | ||||||
| 
 |         //
 | ||||||
|             noteEditor.reload(); |         //     noteEditor.reload();
 | ||||||
|         } |         // }
 | ||||||
| 
 | 
 | ||||||
|         changesToPushCountEl.html(resp.changesToPushCount); |         changesToPushCountEl.html(resp.changesToPushCount); | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										26
									
								
								services/messaging.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								services/messaging.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | |||||||
|  | const utils = require('../services/utils'); | ||||||
|  | const WebSocket = require('ws'); | ||||||
|  | 
 | ||||||
|  | let webSocketServer; | ||||||
|  | 
 | ||||||
|  | function init(httpServer) { | ||||||
|  |     webSocketServer = new WebSocket.Server({server: httpServer}); | ||||||
|  |     webSocketServer.on('connection', function connection(ws, req) { | ||||||
|  |         console.log("websocket client connected"); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function send(message) { | ||||||
|  |     const jsonStr = JSON.stringify(message); | ||||||
|  | 
 | ||||||
|  |     webSocketServer.clients.forEach(function each(client) { | ||||||
|  |         if (client.readyState === WebSocket.OPEN) { | ||||||
|  |             client.send(jsonStr); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  |     init, | ||||||
|  |     send | ||||||
|  | }; | ||||||
| @ -1,6 +1,7 @@ | |||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const source_id = require('./source_id'); | const source_id = require('./source_id'); | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
|  | const messaging = require('./messaging'); | ||||||
| 
 | 
 | ||||||
| async function addNoteSync(noteId, sourceId) { | async function addNoteSync(noteId, sourceId) { | ||||||
|     await addEntitySync("notes", noteId, sourceId) |     await addEntitySync("notes", noteId, sourceId) | ||||||
| @ -35,6 +36,41 @@ async function addEntitySync(entityName, entityId, sourceId) { | |||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | let startTime = utils.nowTimestamp(); | ||||||
|  | let sentSyncId = []; | ||||||
|  | 
 | ||||||
|  | setInterval(async () => { | ||||||
|  |     const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ?", [startTime]); | ||||||
|  |     startTime = utils.nowTimestamp(); | ||||||
|  | 
 | ||||||
|  |     const data = {}; | ||||||
|  |     const syncIds = []; | ||||||
|  | 
 | ||||||
|  |     for (const sync of syncs) { | ||||||
|  |         if (sentSyncId.includes(sync.id)) { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (!data[sync.entity_name]) { | ||||||
|  |             data[sync.entity_name] = []; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         data[sync.entity_name].push(sync.entity_id); | ||||||
|  |         syncIds.push(sync.id); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (syncIds.length > 0) { | ||||||
|  |         messaging.send({ | ||||||
|  |             type: 'sync', | ||||||
|  |             data: data | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         for (const syncId of syncIds) { | ||||||
|  |             sentSyncId.push(syncId); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }, 1000); | ||||||
|  | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     addNoteSync, |     addNoteSync, | ||||||
|     addNoteTreeSync, |     addNoteTreeSync, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner