Home > Article > Backend Development > How do Golang and JS interact?
How do Golang and JS interact?
1. Use AJAX technology, which can update part of the web page without reloading the entire web page;
Use Ajax The biggest advantage is that it can maintain data without updating the entire page. This allows web applications to respond more quickly to user actions and avoids sending unchanged information over the network.
Ajax does not require any browser plug-ins, but requires the user to allow JavaScript to execute on the browser. Just like DHTML applications, Ajax applications must be rigorously tested on many different browsers and platforms. As Ajax matures, some program libraries that simplify the use of Ajax have also come out. Likewise, another assistive programming technology has emerged to provide alternative functionality for users who do not support JavaScript.
The main criticism of using Ajax is that it can break the browser's back and bookmark functionality. In the case of dynamically updated pages, the user cannot go back to the previous page state because the browser can only remember static pages in the history. The possible differences between a page that has been completely read and a page that has been dynamically modified are very subtle; users often expect to click the back button to cancel their previous operation, but in an Ajax application, this is not the case. Unable to do so. However, developers have come up with various ways to solve this problem. Most of the methods before HTML5 were to create or use a hidden IFRAME to reproduce the changes on the page when the user clicks the back button to access the history. (For example, when the user clicks back in Google Maps, it searches in a hidden IFRAME and then reflects the search results onto an Ajax element to restore the application state to what it was at that time).
Regarding the problem of not being able to add status to favorites or bookmarks, one way before HTML5 was to use URL fragment identifiers (often called anchors, the part after # in the URL) ) to keep track and allow the user to return to a specified application state. (Many browsers allow JavaScript to dynamically update anchors, which allows Ajax applications to update anchors while updating the displayed content.) HTML5 will later be able to directly manipulate browsing history, store web page status in the form of strings, and add web pages to web favorites. When clipping or bookmarking, the state is retained invisibly. The above two methods can also solve the problem of being unable to retreat at the same time.
When developing Ajax, network latency—that is, the interval between a user's request and the server's response—needs to be carefully considered. Not giving users a clear response, not properly pre-reading data, or improperly handling XMLHttpRequest will make users feel bored. A common solution is to use a visual component to tell the user that the system is performing background operations and reading data and content.
2. Using WebSocket communication technology, a two-way channel based on TCP connection can be provided between the browser and the server, making it easier to exchange data.
Less control overhead. When data is exchanged between the server and client after a connection is created, the packet headers used for protocol control are relatively small. Without extensions, for server-to-client content, the header size is only 2 to 10 bytes (related to the packet length); for client-to-server content, additional headers need to be added. 4-byte mask. Compared with HTTP requests that carry complete headers every time, this overhead is significantly reduced.
Stronger real-time performance. Since the protocol is full-duplex, the server can proactively send data to the client at any time. Compared with HTTP requests, which need to wait for the client to initiate a request before the server can respond, the delay is significantly less; even compared with similar long polling methods such as Comet, it can deliver data more times in a short period of time.
Stay connected. Unlike HTTP, Websocket needs to create a connection first, which makes it a stateful protocol, and some state information can be omitted during subsequent communications. HTTP requests may need to carry status information (such as identity authentication, etc.) in each request.
Better binary support. Websocket defines binary frames, which can handle binary content more easily than HTTP.
can support extensions. Websocket defines extensions, and users can extend the protocol and implement some customized sub-protocols. For example, some browsers support compression, etc.
Better compression effect. Compared with HTTP compression, Websocket, with appropriate extension support, can inherit the context of previous content and significantly improve the compression rate when transmitting similar data.
Recommended tutorial: "Golang"
The above is the detailed content of How do Golang and JS interact?. For more information, please follow other related articles on the PHP Chinese website!