![]() |
Platforms, Frameworks & Libraries »
Game Development »
Games
Intermediate
Skype Game InfraBy Michael GopshteinUsing Skype as infrastructure for multiplayer games. |
C#, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

If you have written a nice multiplayer game, which can be played only by users on the same computer (as you never had time to even start thinking on making it online) � this article is for you. It presents a simple approach for all those �networking� tasks, and comes with a simple library implementing it. So make you game online in five minutes (well, probably two hours).
For this discussion, I want to divide this task to two separate parts:
The first part is interesting and difficult by itself, but is totally out of the scope of current article. For example, for chess game it includes UI and game engine that will allow two people to play chess on same computer. For my demo application, I use great chess board control, created by Gregory Prentice. In addition to the nice UI, it has a fully functional chess engine which validates the moves, fires events both for UI changes (move) and game notifications (Mate!), and so on. You can read its description here, or download the most recent version from here.
Let�s look deeper into the second task � going online. Here we should look separately at:
Opening connection between two users:
Additional services include chat window, which is a must for a game, and optionally other communication channels � voice over IP, video, etc.
Well, currently this all seems very complicated.
As you can already guess, this paragraph will explain how Skype can ease all those tasks.
I started using Skype couple of years ago for VOIP � talking to my friends and family in different countries in great sound quality. Over the time they added chat and video functionality, including conference calls.
Similar to other programs of this type, you create Skype account, mange list of contacts, see when each of your friends is online etc.
In addition to those features, there�s a Skype Public API, which allows developing new application based on Skype capabilities. The API gives full access to all basic functionality: contacts, chats, voice calls etc. Current solution is mostly based �Application Streams� � data channels which can be created between 3rd-party applications.
Next section describes Skype API in more details. But the concept should be already clear � we will use those data streams to exchange game-specific info between players which will be all connected to Skype. This approach solves most of the difficulties in making online games:
This section describes basic Skype API and a COM API, when special attention is given to topics related to �Application Streams�. The API is well documented, and can be found here. You can also use developers' forum to find answers on subjects that are not explained well in the documentation.
In the next section you can find description of SkypeGameInfra library, which wraps all relevant functionality in convenient way. You can even skip current section for now, and go back to it only when you need to get more details, or make changes.
Application exchanges data with Skype using WM_COPYDATA Windows messages. The format of the messages is called �Skype Protocol�, which defines set of commands that can be sent to Skype. Full description of commands can be found here.
In addition to Skype API, which isn�t very friendly to use, there�s a COM wrapper which represents the Skype API as COM objects with properties and events. Again, full description can be found here. I will describe the parts relevant for creating �Application Streams�.
SKYPE4COMLib.Skype skype = new SKYPE4COMLib.Skype();
This operation will pop-up a message in Skype, saying to the user that new application is attaching. The user should allow this operation (it�s recommended to use the �never ask again� option).
Note: if by mistake you�ve disabled your application from using Skype, you can go to C:\Documents and Settings\WINDOWS USER NAME\Application Data\Skype\SKYPE USER NAME, and find your application in config.xml, UI/AccessControlList/ClientX node � just delete this whole node (make sure the numbering of Client nodes remains correct)
AttachmentStatus property:SKYPE4COMLib.ISkype iskype = skype as SKYPE4COMLib.ISkype;
if (iskype.AttachmentStatus ==
SKYPE4COMLib.TAttachmentStatus.apiAttachNotAvailable)
{ /* error */ }
SKYPE4COMLib.Application app =
skype.get_Application(APPLICATION_NAME);
if (app != null) {
app.Create();
}
ApplicationStreams and ApplicationReceiving. skype.ApplicationStreams += new
SKYPE4COMLib._ISkypeEvents_ApplicationStreamsEventHandler(
skype_ApplicationStreams);
skype.ApplicationReceiving += new
SKYPE4COMLib._ISkypeEvents_ApplicationReceivingEventHandler(
skype_ApplicationReceiving);
app.Connect(skypeHandle, false);
// skypeHandle is just Skype username
// of the user you want to connect to
Note: The connection is received by application registered with the same APPLICATION_NAME as your application. If no such application was registered, no connection will be opened.
ApplicationStreams notification, and from now on you can use the stream to send and receive messages. void skype_ApplicationStreams(SKYPE4COMLib.Application pApp,
SKYPE4COMLib.ApplicationStreamCollection pStreams)
{
SKYPE4COMLib.ApplicationStream stream = null;
foreach (SKYPE4COMLib.ApplicationStream
astream in pStreams)
if (stream == null)
stream = astream;
}
stream.Write(�My message�);
ApplicationReceiving events: void skype_ApplicationReceiving(SKYPE4COMLib.Application pApp,
SKYPE4COMLib.ApplicationStreamCollection pStreams)
{
foreach (SKYPE4COMLib.ApplicationStream
astream in pStreams) {
string msg = astream.Read();
// process message
}
}
As an example, see the GameCommunications class in the SkypeGameInfra project.
In addition to those APIs you can use one of two ActiveX controls to automatically get list of contacts defined in Skype: �SkypeContactList� and �SkypeContactCombo�. The use of those controls is pretty self-explanatory � just add one of them to your Form.
They can be downloaded from here - �Skype ActiveX Tools�
It�s .NET wrapper on top of Skype COM API which encapsulates all functionality needed for a game between 2 players.
DemoSkypeChess is an example project which uses this API.
SkypeGameInfra.GameCommunication skypeWrapper =
new SkypeGameInfra.GameCommunication("DemoChess");
Note: replace �DemoChess� with your unique name
string username = skypeWrapper.SelectUser();
if (username.Length > 0)
skypeWrapper.InviteOpponent(username);
SelectUser opens a dialog with list of all contacts, and the user should select one of them. InviteOpponent opens �Application Stream�, and sends an invitation to selected contact.
GameCommunication object with same name, will receive InvitationReceived event. In the application level you will, probably, want to show a nice dialog to the user, asking to accept or decline the game.
AcceptGame of DeclineGame should be called, and Alex will receive OpponentAccepted or OpponentDeclined event. Starting from this point the communication channel is established: Alex plays white, and Bob � black.
skypeWrapper.Move(position);
position parameter is arbitrary game-specific string, which will be received by the other side in OpponentMove event.
StopGame function. This will release the �Application Steam� channel for both users, and bring the system back to the state it was after sep 1. Chess control by Gregory Prentice:
Skype links:
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 1 Nov 2006 Editor: Smitha Vijayan |
Copyright 2006 by Michael Gopshtein Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |