Automatically Reconnect to the Server and Recover the Document
We just published a sample project that shows how to reconnect to the server and how to recover the current document.

In order to provide the true WYSIWYG rendering and the unique pixel-perfect editing experience, the document is getting synchronized with the server using a WebSocket connection. A constant connection is required in order to maintain the document.
This sample shows how to use the local browser storage to save the document locally in order to recover the document when the connection is lost.
In the textControlLoaded event, the autoSaveDocument function is attached to an interval of 500 milliseconds. This is a background task and will not influence the user editing experience.
TXTextControl.addEventListener("textControlLoaded", function (e) {
// disable the loading progress dialog
TXTextControl.isLoadingDialogEnabled = false;
// restore the document
restoreDocument();
// set auto save to 500 milliseconds
var intervalAutoSave = setInterval(autoSaveDocument, 500);
});
In the autoSaveDocument function, the document is saved in the internal Text Control format and is stored in the local browser session storage. This storage is session-wide and can be accessed browser tab-wide. If you need to implement a "per document" session, you might want to give the session storage entry a unique name.
function autoSaveDocument() {
// store the document in the session storage
// which is shared over all browser tabs (complete session)
TXTextControl.saveDocument(
TXTextControl.streamType.InternalUnicodeFormat, function (e) {
sessionStorage.document = e.data;
}
);
}
In case, the connection is lost, TX Text Control fires the event webSocketClosed. In this event, we simply reload the page in order to reconnect to the server.
TXTextControl.addEventListener("webSocketClosed", function (e) {
// reload, if connection is lost
location.reload();
});
In the textControlLoaded event (see first code snippet), the function restoreDocument is called. This function is loading the stored document from the session storage in case it is available.
function restoreDocument() {
// if document exists, load it from the session storage
if (sessionStorage["document"]) {
$("#txReconnectMsg").show(); // hide dialog
TXTextControl.loadDocument(
TXTextControl.streamType.InternalUnicodeFormat,
sessionStorage.document);
}
else
$("#txReconnectMsg").hide(); // hide dialog
}
Other events are used to show and hide messages informing the user that the application is trying to reconnect to the server. The following snippet shows the complete JavaScript that is required to implement this feature:
TXTextControl.addEventListener("textControlLoaded", function (e) {
// disable the loading progress dialog
TXTextControl.isLoadingDialogEnabled = false;
// restore the document
restoreDocument();
// set auto save to 500 milliseconds
var intervalAutoSave = setInterval(autoSaveDocument, 500);
});
TXTextControl.addEventListener("webSocketClosed", function (e) {
// reload, if connection is lost
location.reload();
});
TXTextControl.addEventListener("documentLoaded", function (e) {
$("#txReconnectMsg").hide(); // show dialog
});
function autoSaveDocument() {
// store the document in the session storage
// which is shared over all browser tabs (complete session)
TXTextControl.saveDocument(
TXTextControl.streamType.InternalUnicodeFormat, function (e) {
sessionStorage.document = e.data;
}
);
}
function restoreDocument() {
// if document exists, load it from the session storage
if (sessionStorage["document"]) {
$("#txReconnectMsg").show(); // hide dialog
TXTextControl.loadDocument(
TXTextControl.streamType.InternalUnicodeFormat,
sessionStorage.document);
}
else
$("#txReconnectMsg").hide(); // hide dialog
}
You can test this sample on your own by downloading the project from our GitHub repository.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2017 or better
- TX Text Control .NET Server (trial sufficient)
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
Creating Your First ASP.NET Reporting Application
This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.
ASP.NET MVC: Implementing a Simplistic, Custom Button Bar
For some applications, the fully-featured ribbon bar might be too overloaded with features or the ribbon concept is not required in a project. Programmatically, all ribbon tabs, groups and buttons…
ASP.NET MVC: Adding Protected Sections to Documents
A SubTextPart object represents a user-defined range of text in a TX Text Control document. A SubTextPart is basically a range of text with a Name and an ID property to store additional…
ASP.NETReportingElectronic Signature
ASP.NET: Adding Electronic Signatures to Documents
An electronic signature is in many processes legally sufficient to prove an identity. According to the U.S. Federal ESIGN Act passed in 2000, an electronic signature is an: Electronic sound,…
HTML5: Display and Handle FormCheckBox Fields
The Text Control Reporting engine MailMerge can populate fields automatically during the merge process. These fields can be also combined with MS Word compatible form fields such as checkboxes.…