Creating Applications with Mozilla-Chapter 12. Remote Applications-P5

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:25

0
43
lượt xem
3
download

Creating Applications with Mozilla-Chapter 12. Remote Applications-P5

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'creating applications with mozilla-chapter 12. remote applications-p5', công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Creating Applications with Mozilla-Chapter 12. Remote Applications-P5

  1. Chapter 12. Remote Applications-P5 12.8. Signed Remote Snake Game In this section, we look at an enhanced version of the Snake game presented earlier in the chapter. The enhanced version uses XPConnect to provide a total full-screen display of the game on the Windows platform as a remote application. 12.8.1. How to Expand Mozilla to Full Screen The best way to expand Mozilla to a full screen mode is through full-screen functions provided in an instance of navigator.xul. These functions run in the Windows build of Mozilla via the Full Screen item in the View menu. These functions also work in Linux and Mac, but do not provide 100% full- screen mode, as some menus and titlebars still show. The problem here is the current window's navigator.xul document, which needs to be accessed to get these full-screen functions. A document loaded in that window just can't use something like window.parent to get to it, so another route must be found. This route runs through the nsIWindowMediator interface by the way of XPConnect. It gives access to the current browser window's navigator.xul document's window object. Example 12-13 includes the code for this window access process, along with the functions used to create the full-screen effect. Example 12-13. Function for switching screen modes
  2. netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect"); const MEDIATOR_CONTRACTID="@mozilla.org/appshell/window- mediator;1"; const nsIWindowMediator=Components.interfaces.nsIWindowMe diator; var windowManager= Components.classes[MEDIATOR_CONTRACTID].getService( nsIWindowMediator); var hideSidebar=true; var isRegular=true; function switchScreen( ) { if(isRegular) { try { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect");
  3. mainWindow = windowManager.getMostRecentWindow("navigator:browse r"); } catch(e) { alert(e); } if(mainWindow.sidebar_is_hidden( )) hideSidebar=false; if(hideSidebar) mainWindow.SidebarShowHide( ); mainWindow.BrowserFullScreen( ); window.fullScreen=true; window.locationbar.visible=false; window.toolbar.visible=false; isRegular=false; } else { try { netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect");
  4. mainWindow = windowManager.getMostRecentWindow("navigator:browse r"); } catch(e) { alert(e); } window.locationbar.visible=true; window.toolbar.visible=true; if(hideSidebar) mainWindow.SidebarShowHide( ); mainWindow.BrowserFullScreen( ); isRegular=true; } } windowManager, which is spawned by XPConnect, creates the mainWindow variable. By using the getMostRecentWindow function for navigator:browser, the Mozilla application window you currently use becomes available. Next, tests are made in code for the window status determine if it is regular or full screen. Appropriate action can then be made by calling the SidebarShowHide function.
  5. As you can see in Example 12-13, code for hiding the toolbar and location bar is also present. This code is accomplished not by the mainWindow created through XPConnect, but by the existing window object: window.locationbar.visible=false; window.toolbar.visible=false; Using both the mainWindow and window objects allows the creation of a full-screen remote Mozilla application by allowing XPConnect privileges. Figure 12-9 shows the result on Windows -- a total full screen for a signed remote Mozilla game! Figure 12-9. Snake game in full-screen mode on Windows
  6. 12.9. Mozilla's XML Extras and SOAP
  7. Mozilla has built functions called XML Extras that allow the use of XML as data in both JavaScript and C++. Such functions are an XML Serializer, XMLHttpRequest, XML Parser, SOAP-based RPC, and XML Persistence. You can find more information about these functions, along with examples, at http://www.mozilla.org/xmlextras/. The following sections assume that you are familiar with SOAP and .NET. If not, some good O'Reilly books available on these subjects can help get you started. 12.9.1. Mozilla, SOAP, and .NET In this section, SOAP is used to access data in a .NET web service, therefore allowing the Snake game to have features such as a saved game score, a retrieved game score, and a list of high scores. As of Mozilla 1.0, the SOAP functions of Mozilla do not work in signed scripts. This bug will be corrected in the future. All JavaScript using SOAP functions in this section is loaded externally of the signed JAR. These SOAP functions do not require enhanced privileges. 12.9.2. Setting Up a .NET Web Service The easiest way to create a .NET web service is through Visual Studio.NET, which provides a template for creating these services. Example 12-14 shows a bare minimum of C# code used to compile the functions that return a value to the Snake game. Obviously, a full implementation would need a database to store these scores. For this section, seeing how the interfaces work for these SOAP functions is more important.
  8. Example 12-14. Minimal .NET web service using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace SnakeService { [WebServiceAttribute (Namespace="uri:SnakeScore")] public class SnakeService : System.Web.Services.WebService { public SnakeService( ) { InitializeComponent( ); } #region Component Designer generated code private IContainer components = null; private void InitializeComponent( ){}
  9. protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose( ); } base.Dispose(disposing); } #endregion [WebMethod] public string SaveScore(string PlayerName, string Score) { return "Save of score successful."; } [WebMethod] public string GetScore(string PlayerName) { int Score = 990; return Score.ToString( ); }
  10. [WebMethod] public string GetHighScores( ) { return "EDM 1000,SLK 200,BRP 10"; } } } The most important part of Example 12-14 is the WebServiceAttribute because it sets up a URI reference to the SnakeService object. When a request is sent from the Snake game to the .NET SnakeService, uri:SnakeScore becomes the name for the object providing functions for getting and setting the game's score. In Example 12-14, all the parameter and return values are of the string type. Considering the brevity of this example, expanding it would not be hard. Functions using other objects and database connections would really make it a true web application. 12.9.3. .NET WSDL .NET automatically generates WSDL interfaces inside a web service. Mozilla SOAP doesn't need to reference a WDSL file to make SOAP transactions. Example 12-15 is a portion of the WDSL that .NET generates and is the specific portion that relates directly to sending raw SOAP calls to the SnakeService. Also, only the definitions for the GetScore function are in this abbreviated definition.
  11. Example 12-15. Abbreviated WSDL as produced by .NET web service.
  12. The most important thing to notice in this WSDL is the soapAction. In Example 12-15, uri:SnakeScore/GetScore is defined as the identifier for the SnakeScore object's GetScore function. This identifier makes the call to this function in Example 12-19. 12.9.4. SOAP Call XML Formats When .NET and Mozilla serialize SOAP calls, they produce different XML formats. The namespace prefixes differ, and Mozilla produces more of these namespaces in its version of the SOAP message. However, the code
  13. comparison in Examples 12-16 and 12-17 mean fundamentally the same thing. Thus, .NET and Mozilla are able to communicate. Example 12-16. XML format for SOAP calls of Mozilla EDM 10
  14. Example 12-17. .NET format for SOAP calls of Mozilla EDM 10 Realizing these formatting differences in Examples 12-16 and 12-17 is important because if you develop with SOAP by using .NET and Mozilla, you are bound to run across variations in your future software projects. Luckily the W3C has set a standard that Mozilla and Microsoft adheres to. 12.9.5. Adding SnakeService SOAP to Snake
  15. Developers use built-in methods to just write JavaScript and use SOAP easily. There is no need for enhanced privileges because nothing could affect the client adversely. However, there are some limitations on how SOAP JavaScript is used. Mozilla sets one level of security with the SOAP function by requiring that the web service and the JavaScript file that makes the SOAP functions access that service be on the same domain. For example, you will not encounter problems when running everything as localhost (as shown in the examples). If you try to move the JavaScript files to mozdev.org, though, they will no longer work. Another limitation of the SOAP functions is that they can't be used directly in XUL documents. However, a hack, discussed in the next section, can get around this limitation. 12.9.6. Make SOAP Functions Work in XUL Documents The best way to circumvent the SOAP-in-XUL-documents problem in Mozilla 1.0 (and probably 1.1) is to initially load the JavaScript file containing the SOAP functions from an HTML file, as shown in Example 12-18. Example 12-18. Preloading scores.js into cache with an HTML association
  16. window.location.href="jar:http://localhost/nss/bin/ snake.jar!/scores.xul"; As stated earlier, scores.js (containing the SOAP functions) must exist outside the JAR file. It is loaded up into cache with this HTML document, and then the page redirects to the XUL file that has the SOAP function user interface. That JavaScript file is already loaded up in cache and will work fine. Remember that doing this is a hack, but later versions of Mozilla that fix the SOAP-in-XUL-document problem would still not break this code. 12.9.7. Examining SOAP Functions for Snake Example 12-19 shows how to create two functions (SaveScore and SaveScoreResponse) to handle SOAP transactions with the previously examined .NET web service. Example 12-19. SaveScore SOAP function const soapVersion = 0; // Version 1.1 const object = "uri:SnakeScore"; const transportURI = "http://localhost/SnakeService/SnakeService.asmx";
  17. // SAVE PLAYER SCORE function SaveScore( ) { var Score = window.opener.document.getElementById("currentscore ").getAttribute("value"); var PlayerName = document.getElementById("saveInitials").value; var method = "SaveScore"; var headers = new Array( ); var params = new Array(new SOAPParameter(PlayerName,"PlayerName"), new SOAPParameter(Score,"Score")); var call = new SOAPCall( ); call.transportURI = transportURI; call.actionURI = object+"/"+method; call.encode(soapVersion,method,object,headers.lengt h,headers,params.length,params); var currentRequest = call.asyncInvoke(SaveScoreResponse); }
  18. function SaveScoreResponse(resp,call,status) { // Display confirmation // Part of content of SOAP message returned alert(resp.body.firstChild.firstChild.firstChild.da ta); } The object defined here is the same as the namespace defined in Example 12-14. Again, this snake score object (uri:SnakeScore) is simply an identifier to that exact web service. The transportURI is the location of the web service. As you can see here, it runs localhost along with the files for the Snake remote Mozilla application. Moving into the actual SaveScore function, a PlayerName is pulled from a in the XUL. The method is the name of the function in the .NET web service with which this code will communicate. headers is an empty array because no SOAP headers are needed for this simple example. Two SOAPParameters are also defined here in an array, and they are just simple strings. Moving on down the code in Example 12-19, a new SOAPCall( ) is defined into the call variable. Two URIs are set up for this SOAPCall object: call.transportURI and call.actionURI, which combines an object and a method into one string. The next two lines, encode and asyncInvoke the SOAPCall and the encoded XML message, as shown
Đồng bộ tài khoản