This page describes how to use weinre to debug your own web pages on a remote device.
First, some background.
When using weinre, there are three programs interacting with each other.
Both the Debug Client and the Debug Target communicate to the Debug Server via HTTP using XMLHttpRequest (XHR).
Typically, you run both the Debug Client and the Debug Server on your desktop/laptop, and the Debug Target on your mobile device.
When using the Mac application, the Debug Client and Debug Server are running in the same process. When you launch the application, the server starts, and then the browser included in the application is launched on the appropriate URL automagically.
When using the weinre.jar program instead of the Mac application, only the Debug Server is started. You will need to launch the Debug Client in a web browser.
The Debug Target is the squirrelliest bit. You will need to arrange to inject some JavaScript code, provided by the Debug Server, into your web page, in order for it to function as a Debug Target.
There are two ways to run the weinre debug server:
To run weinre from the jar file, execute the following command:
java -jar path/to/weinre.jar [options]
Options available are:
Display this help.
The port to run the HTTP server on.
Default: 8080
The ip address to bind the server to.
Default: localhost
With the default of localhost, you won't be able to access the server from any machine other than the one the server is running on. You will need to bind to another hostname / ip address to make the server accessible to other machines. You can use the value -all- to indicate that you want to bind to all interfaces available on the current machine.
Use ifconfig on Linux or the Mac, and ipconfig on Windows, to get the ip addresses available on your machine.
Causes jibberish to be written to stdout
Default: false
Sets the "reuseAddr" flag for Jetty.
Default: false
May be needed if you restart your server frequently.
The number of seconds the server will wait for a message to be available to send to the target or client.
Default: 5
Can probably be set larger, but then your browser may time out. Probably don't want to set it much lower than this as it will cause additional network traffic when no messages are being sent (it will cause more "empty" messages to be sent).
The number of seconds to wait to hear back from a debug client or target before declaring them dead.
Default: 3 * readTimeout
Normally the default will be fine. However, if you're debugging weinre itself, you may wish to set it much longer.
You may also create a file named server.properties in the directory .weinre in your home directory (eg. ~/.weinre/server.properties). The keys should be the same as the name of the options above, with the same values.
As an example, your ~/.weinre/server.properties file might look like this:
boundHost: -all- httpPort: 8081 reuseAddr: true readTimeout: 1 deathTimeout: 5
Command-line options override the options specified in the ~/.weinre/server.properties file.
The server will run until you kill it. Control-C will do the job.
Go back and read the description above about the weinre.jar settings.
Only the ~/.weinre/server.properties file is available to configure the server, as there is no traditional command line used to launch Mac "applications".
The server will run until you quit the application.
Since there is no practical way to see the stdout and stdin from the server when running a Mac application, these streams are intercepted and displayed in the Server Console tab in the Mac application.
The Mac application also displays the server's home page in a separate tab.
Since the web page you want to debug (the debug target) is probably running on a different machine (your mobile device) than the debug server (your desktop / laptop), you can't use the default localhost value for the --boundHost option. Instead, you will need to specify a host name / ip address with the --boundHost option. This host name / ip address must be accessible to the machine you want to use as the debug target.
To get started, you can use the value -all- for the --boundHost option.
Now you'll need to figure out what host name / ip address the debug target should use. If you've use the --boundHost -all- option, then bring up the debug client using the URL http://localhost:[portNumber]/client/ from the same machine the debug server is running on. On that page, under Server Properties, is a list of bound hosts that the server believes it's available on.
From the debug target device (eg, your mobile device), try accessing the server home page from all of those host names, using the URL http://[host name / ipaddress]:[portNumber]/. Once you find one that displays the server's home page correctly, remember that host name / ip address, we'll be using it in a minute.
When you run the debug server with a bound host other than localhost, by definition other machines can now access your debug server. See the Security page for information on how to protect yourself.
At this point you now have a URL to the debug server which will be accessible from the debug target. Let's say that URL (to the debug server's home page) is: http://a.b.c:8081.
To make your web page debuggable with weinre, add the following line to your web page:
<script src="http://a.b.c:8081/target/target-script-min.js"></script>
You can now open a debug client by browsing to http://a.b.c:8081/client (or probably http://localhost:8081/client as well) on your desktop/laptop, then reload your newly instrumented web page on your mobile device, and see the two connect in the Remote panel of the debug client.
Yeah, gross.
Besides being gross, there are a few practical problems with hardcoding the debug server address in your web page as well:
But you don't want to be keeping the <script src=""> line in your SCM either. You should look for a way, as part of your personal build process to add that line only for development builds, and presumably with a user-customizable URL.
One way out of the dilemma is to use a dynamic DNS solution for the the machine you run the debug server on. Doing a web search on the following terms will provide you some links to such services: dynamic dns static hostname
If you use one of these dynamic DNS services, you can arrange to use the same host name for your debug server no matter what network you are connected to.
Some platforms support 'bookmarkets', which allow you to inject arbitrary JavaScript code into any web page running in your browser. Mobile Safari is one of these browsers. It appears that the browser shipped with Android 2.2 and later also supports bookmarklets.
Unfortunately, installing bookmarkets on mobile devices isn't usually trivial. The easiest course of action is to add a new random bookmark to your bookmarks, and then edit it by hand to change the label and URL. The URL you should use is available in both a 'pre' and 'textarea' section of the server's home page (eg, http://localhost:8081/). You should be able to select the text from one of these areas easily, to paste into the previously added bookmark.
Once the bookmarklet is installed, you can debug any web page by visiting it, and selecting the bookmarklet from your bookmarks menu.
If you'd like to use the debug target code statically, instead of downloading it from the server all the time, then you will need to do one more thing. The debug target code computes the address of the debug server from the src attribute in the <script> tag, and it won't be correct in the static case. Instead you can set the following variable to be the URL of the debug server:
window.WeinreServerURL
This URL overrides what's otherwise calculated from the debug target code itself. You will need to set this value before the target/target-script-min.js script is loaded.
The calculation of the debug server url by the debug target is a bit fragile and ham-fisted. We should look at additional ways to handle this. For instance, we could prompt the user for the debug server address, and then always using the debug target in a more static mode; eg, you'd include the target-script-min.js with all your other files in your application.
Running the debug server on the target device is another option. This shouldn't be a huge problem for Android, but is more of a problem on iOS. By running the debug server on the target device, the debug target can always connect to the debug server by using a localhost hostname. The debug client, running on your desktop/laptop, would then need to connect to the target device to debug. In practice this may be an easier scenario to work with, as the potentially changing IP address of the mobile device just means having to type a different URL in your web browser on your desktop / laptop when running the debug client. That's easier than hard-coding URLs in your web page, for instance.