1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Creating Applications with Mozilla-Chapter 12. Remote Applications-P5 doc

25 297 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Signed remote snake game
Thể loại Chapter
Định dạng
Số trang 25
Dung lượng 76,06 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.. Example 12-13 inclu

Trang 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

Trang 2

"UniversalXPConnect");

const

mediator;1";

MEDIATOR_CONTRACTID="@mozilla.org/appshell/window-const

nsIWindowMediator=Components.interfaces.nsIWindowMediator;

var windowManager=

Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);

Trang 3

mainWindow =

windowManager.getMostRecentWindow("navigator:browser");

Trang 4

mainWindow =

windowManager.getMostRecentWindow("navigator:browser");

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

Trang 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:

Trang 6

12.9 Mozilla's XML Extras and SOAP

Trang 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

Trang 8

Example 12-14 Minimal NET web service

Trang 9

protected override void Dispose( bool

Trang 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

Trang 11

Example 12-15 Abbreviated WSDL as produced by NET web service

<?xml version="1.0" encoding="utf-8"?>

<definitions

xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema"

xmlns:s0="uri:SnakeScore"

xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"

xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"

xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" targetNamespace="uri:SnakeScore"

xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

<s:schema elementFormDefault="qualified"

targetNamespace="uri:SnakeScore"> <s:element name="GetScore">

Trang 14

</port>

</service>

</definitions>

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

Trang 15

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

<env:Envelope

xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/"

env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

Trang 16

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

12.9.5 Adding SnakeService SOAP to Snake

Trang 17

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

Trang 18

<body>

<script>

window.location.href="jar:http://localhost/nss/bin/snake.jar!/scores.xul";

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";

Trang 19

// SAVE PLAYER SCORE

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.length,headers,params.length,params);

var currentRequest =

call.asyncInvoke(SaveScoreResponse);

}

Trang 20

}

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 <textbox> 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

Trang 21

in Examples Example 12-16 and Example 12-17, are sent to the NET web service When a response is received, the SaveScoreResponse function

is called

Currently, a hack is used in SaveScoreResponse so a DOM property accesses the XML of the returned SOAP message This implementation is not the best way, but the optimal way doesn't currently work Here is an easy version of the code:

ret = resp.getParameters(false, new Array( ));

alert(ret[0]);

If you put this code in SaveScoreResponse and it works, it could

replace the code in Examples 12-16 and 12-17 You need to play around with these different SOAP functions and see what works for you Again, the code just shown does not work in Mozilla 1.0, but will hopefully work in all future versions of Mozilla

Example 12-20 shows the code for GetResponse and GetHighScores

Compare the JavaScript code to the code and WSDL in Examples 12-14 and 12-15 to see how they all work together

Example 12-20 Code for GetScore and GetHighScores

// GET PLAYER SCORE

var ScoreElement; // Make this accessible to

GetScoreResponse

function GetScore( )

{

Trang 22

var method = "GetScore";

var headers = new Array( );

var params = new Array(new

Trang 23

alert("Your score has been reinstated You can now return to the game.");

}

// GET HIGH SCORES

function GetHighScores( )

{

var method = "GetHighScores";

var headers = new Array( );

var params = new Array( );

var call = new SOAPCall( );

call.transportURI = transportURI;

call.actionURI = object+"/"+method;

call.encode(soapVersion,method,object,headers.length,headers,params.length,params);

var currentRequest =

call.asyncInvoke(GetHighScoresResponse);

}

function GetHighScoresResponse(resp,call,status) {

alert(resp.body.firstChild.firstChild.firstChild.data);

Trang 24

}

Figure 12-10 shows how the XUL interface to these functions in Example 12-20 is designed Here the score is replaced with "990," as this number is pulled from the code shown in Example 12-14

Figure 12-10 Result of using the GetScore function

Trang 25

12.10 Looking Forward

This chapter focuses on just one of many new trends outside of the original project mandate that emerged in the Mozilla developer community Now that Mozilla 1.0 is released, its future direction will be shaped by the

community itself, and Mozilla will become whatever the community would like it to be

Remote applications are definitely one area of Mozilla development that will get more attention as Mozilla matures Other areas that will probably also be noticed include development tools (some existing development tools are discussed in Appendix B), embedding, SVG support, and XSLT support

Remember that Mozilla is open to new ideas and is always looking for

contributions If you can think of a way to improve Mozilla, or if you think

of something that should be added, become a part of the community and help expand the possibilities of Mozilla and all Mozilla applications

Ngày đăng: 26/01/2014, 07:20

TỪ KHÓA LIÊN QUAN