Current location - Quotes Website - Team slogan - The web side wakes up the local application and detects whether the wake-up is successful.
The web side wakes up the local application and detects whether the wake-up is successful.
Web can call local application through customized URL protocol. We just need a link like this:

There are many related blogs on the Internet about the registry mentioned above and why local applications can be called through the custom URL protocol. I will post some articles that I think are more detailed and clear, so I won't introduce the principle in detail.

Start a local application in a web page through a custom URL protocol.

How to open PC local application on front-end webpage?

However, none of the above is the focus of today. The focus today is how do we know whether we have successfully awakened the local application.

If you have ever searched this problem on the Internet, then the final solution you got is probably him-protocolcheck.js.

Protocolcheck.js sorts out the hacks corresponding to each browser according to the different reactions of various browsers when calling the local application through the customized protocol, and detects whether the browser successfully wakes up the application by detecting the browser used by the user to call the corresponding hacks:

Although the protocol checks. Js has encapsulated the detection methods corresponding to the browser just now. Most of these methods are hacks after all, and have not been updated for three or four years. With the update of the browser, many problems will certainly be gradually exposed (the latest versions of chrome and Firefox have failed).

Obviously, this is not a good solution. Just when I want to discuss the feasibility of this demand with the product manager, I feel that Baidu's network disk seems to have similar functions, so how is Baidu's network disk realized?

I have studied the wake-up function of Baidu's network disk for a period of time, and found that he will send the same request continuously during the wake-up process, and will not send it again after the wake-up is successful. Looking at the response of the request, we can see that the request returned three fields, namely errno, request_id and status. Among them, when the wake-up is successful, all other requests have a status of 2 except the status of the last request before the wake-up is successful.

When the wake-up fails, the status of all requests is 2.

It is not difficult to judge that Baidu Cloud Disk judges whether the wake-up is successful through direct communication between the web and the client, but how do they communicate? We can't press the table first, but this way reminds me of the quick login function of EN and QQ mailboxes. When we start the QQ client and then visit the QQ email login page, the page will prompt us to log in to QQ, and click to log in.

Response content

This request returned login information, and it was obtained from127.0.0.1:4301.So the question is, who provides this service?

The truth is out! From the above information, we can judge that the client of QQ has opened the HTTP server. When the browser opens the page, it will send a request to this server to judge whether the user is logged in and return the corresponding user information.

Premise: The local application to be awakened is self-developed.

Through the case of Baidu cloud disk and fast login, we can get the final solution to wake up local applications:

The problem seems to have been solved, but we actually need to solve the cross-domain problem between the web and the local application HTTP server:

Scheme 1: through the background agent, this is the way to use Baidu cloud disk;

Scheme 2: through jsonp, this is a way to log in to qq mailbox quickly;

The cost of the first scheme is high, and it needs to add an additional proxy layer, and it needs to find a way to generate a unique identifier to mark the current browser; Scheme 2 has no unique identification problem, but the port number needs to be agreed in advance, and the port number may be occupied. Finally, considering the cost and the probability that the port is occupied, I chose the second scheme, which is not perfect, but can meet the needs of most users.