Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 25 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
25
Dung lượng
76,06 KB
Nội dung
Chapter 12.RemoteApplications-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
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");
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");
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.
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
12.9. Mozilla's XML Extras and SOAP
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.
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( ){}
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( );
}
[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.
[...]... 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. .. 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... 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"; // SAVE PLAYER SCORE function SaveScore( ) { var Score = window.opener.document.getElementById("currentscore ").getAttribute("value"); var PlayerName = document.getElementById("saveInitials").value;... 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... ... 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 Developers use built-in methods to just write JavaScript and use SOAP... 12-20 Code for GetScore and GetHighScores // GET PLAYER SCORE var ScoreElement; // Make this accessible to GetScoreResponse function GetScore( ) { ScoreElement = window.opener.document.getElementById("currentscore "); var PlayerName = document.getElementById("getInitials").value; var method = "GetScore"; var headers = new Array( ); var params = new Array(new SOAPParameter(PlayerName,"PlayerName")); var... alert(resp.body.firstChild.firstChild.firstChild.da ta); } 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 12.1 0 Looking Forward This chapter focuses on just one of many new trends outside of the original project mandate that emerged in the Mozilla... 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 Remoteapplications 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... 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 . Chapter 12. Remote Applications- P5
12. 8. Signed Remote Snake Game
In this section, we look at an enhanced. window's
navigator.xul document's window object. Example 12- 13
includes the
code for this window access process, along with the functions used