
Our new chalenge will be the creation of a multiplayer system for our application. We would like that,by example, when some user at the computer A opens the door, the door will be open at the application running at the computer B, in any other place of the world. The same idea is valid for some other things that change their "status" : a money-bag is not available anymore because a player found it etc.
We will try also - at the next lesson - to enhance the application to accept many avatares. It's possible also to create a chat...
Our premisse is that the programer (you) MAY NOT have a private sophisticated server (created using sockets etc.) and will need to use a commercial webhoster having only suport for a DB Microsoft Access (.MDB files). Like: NTT/Verio - US $20/mo - or the free Brinkster.
We need first to create an Accsess DB whose name will be: ServerTu.mdb.
The first table in the DB will have the name: objstatus with 2 columns:
code CHAR 3
status CHAR 1
The code for our door will be:"001" and the "status" = F means that the door is NOT open; T if open.
This DB goes to the folder:data of our webhoster.
We will use a WebService. Our option is the very simple use of a .ASMX file and WSDL. They are supported by Indigo.
The file ServerDB.asmx defines 3 methods using ADO .NET to access our table. Look:
<%@ WebService Language="C#" class="ServerDB"%>
using System;
using System.Text;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
public class ServerDB:WebService {
public ServerDB(){
}
[WebMethod]
public String Door001Sit() {
String select = "SELECT * FROM objstatus WHERE code='001'";
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter datadapt = new OleDbDataAdapter(select, oConn);
datadapt.Fill(ds, "dor");
return ds.Tables["dor"].Rows[0]["status"].ToString();
oConn.Close();
}
[WebMethod]
public String Opened001() {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE objstatus SET status='T' WHERE code='001'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "Door 001 open!!";
}
[WebMethod]
public String Closed001() {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE objstatus SET status='F' WHERE code='001'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "Door 001 closed";
}
}
We place this file at our webhoster and, creating a folder tu14p at our computer, we open the ComandLine Window, we go to this folder and run:
WSDL http://www.yoursite.com/ServerDB.asmx?WSDL
You need to liberate the firewall. In the folder tu14p will be created the "proxy": ServerDB.cs
Create the file Tu14p.csproj:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>Tu14p</AssemblyName>
<TargetType> library</TargetType>
<Configuration>Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<RootNamespace>Tu14p</RootNamespace>
<OutputType>library</OutputType>
<TargetType>$(OutputType)</TargetType>
<OutputPath>C:\Tu14p</OutputPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
<ItemGroup>
<Compile Include="ServerDB.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
</Project>
Compile using the old MSBUILD and you will have a new file: Tu14p.dll. This file needs to go to the folder of the applicattion (Tu15v at this lesson).
TIP:Create a folder having the name of Tu14p and download to it and unzip:
You have there all the files of the DB and the WebService.
After this "express course" of WebService and Indigo, lets go back to our application.
At the .CSPROJ - of our application! - you need, to liberate the access to the Tu14p.dll (copied in the Tu15v folder) and, by it, to the methods to read/update the DB that are in the server.Include:
... <ItemGroup> <Reference Include="Tu14p, Culture=neutral, processorArchitecture=MSIL"> <HintPath>Tu14p.dll</HintPath> <Name>Tu14p</Name> <SpecificVersion>False</SpecificVersion> <Aliases>Global</Aliases> </Reference> </ItemGroup> ...
The complete file is in the download tu15v.zip - at the end of the lesson.
In the code-behind we need to add some lines:
1 - Now we are working with DB, so:
Using Data.OleDB;
2 - To create an object of the class ServerDB (that is in the server but can be accessed here - this is the magic of Indigo!):
ServerDB sDB = new ServerDB();
3 - We will change the OnTimerEvent. At each 10 seconds it will run the method sDB.Door001Sit - on the WebService in the webhoster - and change the position of the door, if needed, syncronizing all the users of our application - WOW! it will be multiplayer now!:
...
void OnTimerEvent(object sender, System.EventArgs args){
if(cont!=999) Text1.TextContent = cont.ToString() ;
if(cont==999) Text1.TextContent = "Network Syncronization..." ;
if(cont==1000)cont=0;
cont=cont+1;
if(cont==1000){
if(sDB.Door001Sit()=="T" && op==0){
open001();
op=1;
}
if(sDB.Door001Sit()=="F"&& op==1){
close001();
op=0;
}
}
}
...
4 - At the lines of code of the capture of the 'meshclick" we need to do some modifications:
...
if (modelHit != null){
GeometryModel3D gm3D = modelHit as GeometryModel3D;
if(gm3D.Geometry == TheCanvas.Resources["buttonG"]){
if(op==0){
open001();
Text1.Text = sDB.Opened001() ;
op=1;
}
else{
close001();
Text1.Text = sDB.Closed001() ;
op=0;
}
return HitTestResultBehavior.Stop;//Exit
}
}
return HitTestResultBehavior.Continue;
}
...
When an user opens or closes the door, one of the methods of the WebService runs in the webhoster computer and updates the database.
OK. I don't know if I talk about everything. Better that you do the download, run the application, analise the code, try to change something etc.
Create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :
TIP: After the compilation using the Visual C# Express, run Tu15v.exe 2 times. WAIT THE CONTACT WITH OUR SERVER (THE DMU SERVER) AFTER THE FIRST SYNCHRONIZATION. Open the door at one window and wait the "Network Syncronisation" (each 10 seconds) to see the door opening in the other.
NOTES: 1 - You need to liberate the firewall for the access to our webhoster, for this application. 2 - Starting the application, wait some seconds for the comunication to the server. 3 - At the moment of the synchronisation the avatar doesn't move. We will present a solution for this problem using threads. 4 -REMEMBER THAT MANY READERS OF THIS TUTORIAL CAN BE TESTING THE APPLICATION AT HE SAME TIME AND THE DOOR CAN HAVE SURPRISED STATUS!!!! IT'S A MULTIPLAYER REALTIME DEMO !!!!!
If you like only to see the application running you don't need to do the above download. Only liberate the firewall and play this web3d application (wait the contact with the server at the 999 in the counter (synchronization), liberate the firewall etc.). Click this button again to create another copy of the application.Close/open the door at a copy and at the new 999 it will change at the other copy.:
![]() |
We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
