mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	websocket stuff separated into messaging.js
This commit is contained in:
		| @@ -140,68 +140,3 @@ function initAjax() { | ||||
| } | ||||
|  | ||||
| initAjax(); | ||||
|  | ||||
| function messageHandler(event) { | ||||
|     console.log(event.data); | ||||
|  | ||||
|     const message = JSON.parse(event.data); | ||||
|  | ||||
|     if (message.type === 'sync') { | ||||
|         lastPingTs = new Date().getTime(); | ||||
|         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(); | ||||
|         } | ||||
|  | ||||
|         const changesToPushCountEl = $("#changesToPushCount"); | ||||
|         changesToPushCountEl.html(message.changesToPushCount); | ||||
|     } | ||||
| } | ||||
|  | ||||
| let ws = null; | ||||
|  | ||||
| function connectWebSocket() { | ||||
|     // use wss for secure messaging | ||||
|     ws = new WebSocket("ws://" + location.host); | ||||
|     ws.onopen = function (event) {}; | ||||
|     ws.onmessage = messageHandler; | ||||
|     ws.onclose = function(){ | ||||
|         // Try to reconnect in 5 seconds | ||||
|         setTimeout(() => connectWebSocket(), 5000); | ||||
|     }; | ||||
| } | ||||
|  | ||||
| connectWebSocket(); | ||||
|  | ||||
| let lastPingTs = new Date().getTime(); | ||||
| let connectionBrokenNotification = null; | ||||
|  | ||||
| setInterval(async () => { | ||||
|     if (new Date().getTime() - lastPingTs > 5000) { | ||||
|         if (!connectionBrokenNotification) { | ||||
|             connectionBrokenNotification = $.notify({ | ||||
|                 // options | ||||
|                 message: "Lost connection to server" | ||||
|             },{ | ||||
|                 // settings | ||||
|                 type: 'danger', | ||||
|                 delay: 100000000 // keep it until we explicitly close it | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
|     else if (connectionBrokenNotification) { | ||||
|         await connectionBrokenNotification.close(); | ||||
|         connectionBrokenNotification = null; | ||||
|  | ||||
|         showMessage("Re-connected to server"); | ||||
|     } | ||||
| }, 3000); | ||||
							
								
								
									
										68
									
								
								public/javascripts/messaging.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								public/javascripts/messaging.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| "use strict"; | ||||
|  | ||||
| const messaging = (function() { | ||||
|     function messageHandler(event) { | ||||
|         console.log(event.data); | ||||
|  | ||||
|         const message = JSON.parse(event.data); | ||||
|  | ||||
|         if (message.type === 'sync') { | ||||
|             lastPingTs = new Date().getTime(); | ||||
|             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(); | ||||
|             } | ||||
|  | ||||
|             const changesToPushCountEl = $("#changesToPushCount"); | ||||
|             changesToPushCountEl.html(message.changesToPushCount); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     let ws = null; | ||||
|  | ||||
|     function connectWebSocket() { | ||||
|         // use wss for secure messaging | ||||
|         ws = new WebSocket("ws://" + location.host); | ||||
|         ws.onopen = function (event) {}; | ||||
|         ws.onmessage = messageHandler; | ||||
|         ws.onclose = function(){ | ||||
|             // Try to reconnect in 5 seconds | ||||
|             setTimeout(() => connectWebSocket(), 5000); | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     connectWebSocket(); | ||||
|  | ||||
|     let lastPingTs = new Date().getTime(); | ||||
|     let connectionBrokenNotification = null; | ||||
|  | ||||
|     setInterval(async () => { | ||||
|         if (new Date().getTime() - lastPingTs > 5000) { | ||||
|             if (!connectionBrokenNotification) { | ||||
|                 connectionBrokenNotification = $.notify({ | ||||
|                     // options | ||||
|                     message: "Lost connection to server" | ||||
|                 },{ | ||||
|                     // settings | ||||
|                     type: 'danger', | ||||
|                     delay: 100000000 // keep it until we explicitly close it | ||||
|                 }); | ||||
|             } | ||||
|         } | ||||
|         else if (connectionBrokenNotification) { | ||||
|             await connectionBrokenNotification.close(); | ||||
|             connectionBrokenNotification = null; | ||||
|  | ||||
|             showMessage("Re-connected to server"); | ||||
|         } | ||||
|     }, 3000); | ||||
| })(); | ||||
| @@ -137,8 +137,6 @@ async function updateNote(noteId, newNote, ctx) { | ||||
|         await encryptNote(newNote, ctx); | ||||
|     } | ||||
|  | ||||
|     const origNoteDetail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); | ||||
|  | ||||
|     const now = utils.nowTimestamp(); | ||||
|  | ||||
|     const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); | ||||
|   | ||||
| @@ -327,6 +327,7 @@ | ||||
|  | ||||
|     <script src="javascripts/link.js"></script> | ||||
|     <script src="javascripts/sync.js"></script> | ||||
|     <script src="javascripts/messaging.js"></script> | ||||
|     <script src="javascripts/utils.js"></script> | ||||
|   </body> | ||||
| </html> | ||||
		Reference in New Issue
	
	Block a user