8,998

Views

Expert

Level

7

Comments

Multiplayer Games

by Jon (Updated on 2013-04-09)


Note: This article is for extremely advanced users. It's a holdover from the older documentation and was written by a 3rd party.

If you'd like to talk with the original author, the forum topic is located here
.

 

Contents

  • What's a socket server?
  • Where can I get one? Are they free?
  • How do I use it with Stencyl?
  • Will it require coding? (YES)
  • Example


 

What is a socket server?

A socket server is different from a standard web server. A web server is designed to handle lots of traffic from different users who make occasional requests every few seconds. By contrast, a socket server is designed for heavy traffic users (like multiplayer game players) who are going to be sending constant traffic back and forth. It opens a dedicated channel for each user and listens to their feedback. Virtually all MMOs and other real-time multiplayer games use socket servers to communicate with their client applications.

 

Where can I get one? Is it free? How do I use it?

There are various commercial socket servers designed for game development. Popular ones are SmartFox, Electrotank and Photon. Each of these servers has a free version that can host between 50-100 concurrent users. There are tutorials and example files for each server on their respective website. A server can be run locally and/or installed to a remote host that allows that type of heavy traffic.

 

How do I use it with Stencyl?

Stencyl can be used to develop client-side applications for multiplayer games. To do this you will have to install a SWC file, which requires modifying Stencyl. Once the SWC is installed, you will be able to use networking code in Stencyl's Code Mode.

  • ElectroServer 5 - Electroserver5.swc
  • SmartfoxServer 2X - SFS2X_API_AS3.swc

You will also need to create a reference to the location of the socket server which would be placed in the "Stencyl\plaf\flash" folder where your Stencyl game is compiled. Examples:

  • ElectroServer 5 - settings.xml
  • SmartfoxServer 2X - config/sfs-config.xml

When you move your client-side application to a webserver, be sure to move the reference file as well along with any necessary assets.

 

Will this require programming?

Yes. In addition to properly setting up your server and designing your game in Stencyl's Code Mode, you will need to write server-side code to handle some game logic. Games need code on the server-side to allow database connectivity and prevent cheating. Java is a common server-side language, but you can use C# or other languages with some servers.

 

Sample Connection Test (on ElectroServer 5)

From these tutorials:
Using ElectroServer with Flash - Part 1
Using ElectroServer with Flash - Part 2

package scripts
{
import com.electrotank.electroserver5.api.CreateRoomRequest;
import com.electrotank.electroserver5.api.JoinRoomEvent;
import com.electrotank.electroserver5.api.LoginResponse;
import com.electrotank.electroserver5.api.PublicMessageEvent;
import com.electrotank.electroserver5.api.PublicMessageRequest;
import com.electrotank.electroserver5.api.UserUpdateAction;
import com.electrotank.electroserver5.api.UserUpdateEvent;
import com.electrotank.electroserver5.util.ES5TraceAdapter;
import com.electrotank.electroserver5.zone.Room;
//import flash.display.Sprite;
//import flash.events.Event;

//ElectroServer imports
import com.electrotank.electroserver5.api.ConnectionResponse;
import com.electrotank.electroserver5.api.LoginRequest;
//import com.electrotank.electroserver5.api.LoginResponse;
import com.electrotank.electroserver5.api.MessageType;
import com.electrotank.electroserver5.ElectroServer;
//import com.electrotank.electroserver5.util.ES5TraceAdapter;

//Logger imports
import com.electrotank.logging.adapter.Log;
import com.electrotank.logging.adapter.ILogger;

import flash.events.*;
import flash.net.*;
import flash.filters.*;
import flash.display.BitmapData;

import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;

import stencyl.api.data.*;
import stencyl.api.engine.*;
import stencyl.api.engine.actor.*;
import stencyl.api.engine.behavior.*;
import stencyl.api.engine.bg.*;
import stencyl.api.engine.font.*;
import stencyl.api.engine.scene.*;
import stencyl.api.engine.sound.*;
import stencyl.api.engine.tile.*;
import stencyl.api.engine.utils.*;
import org.flixel.*;

public dynamic class ConnectToES5 extends SceneScript
{
//Expose your attributes to Stencyl like this
[Attribute(id="1", name="Display Name", desc="An Attribute")]
public var attributeName:String;
//Then in the constructor or init(), add it to the nameMap like this
//nameMap["Display Name"] = "attributeName";
//This lets API calls using attribute names to use the display name

//add this so we can see the logs get traced
Log.setLogAdapter(new ES5TraceAdapter);

private var _room:Room;

//create a new ElectroSerserver instance to gain access to the API
private var _es:ElectroServer = new ElectroServer();

//Do all actor initialization here
override public function init():void
{
//load settings file and connect
_es.loadAndConnect("settings.xml");

//listen to key events to know when a connection has succeeded (or failed), and when login has succeeded (or failed)
_es.engine.addEventListener(MessageType.ConnectionResponse.name, onConnectionResponse);
_es.engine.addEventListener(MessageType.LoginResponse.name, onLoginResponse);
_es.engine.addEventListener(MessageType.JoinRoomEvent.name, onJoinRoomRequest);
_es.engine.addEventListener(MessageType.PublicMessageEvent.name, onPublicMessageEvent);
_es.engine.addEventListener(MessageType.UserUpdateEvent.name, onUserUpdateEvent);
}

private function onUserUpdateEvent(e:UserUpdateEvent):void
{
switch(e.action) {
case UserUpdateAction.AddUser:
print(e.userName + " has joined room " + e.roomId.toString() + " in zone: " + e.zoneId.toString());
break;
case UserUpdateAction.DeleteUser:
print(e.userName + " has left room " + e.roomId.toString() + " in zone: " + e.zoneId.toString());
break;
}

}

private function onPublicMessageEvent(e:PublicMessageEvent):void
{
print(e.userName + ": " + e.message);
}

private function onJoinRoomRequest(e:JoinRoomEvent):void
{
_room = _es.managerHelper.zoneManager.zoneById(e.zoneId).roomById(e.roomId);

sendChatMessege();
}

private function sendChatMessege():void
{
var pmr:PublicMessageRequest = new PublicMessageRequest();
pmr.message = "Hello World";
pmr.roomId = _room.id;
pmr.zoneId = _room.zoneId;

_es.engine.send(pmr);
}

private function onConnectionResponse(e:ConnectionResponse):void {
print("Connection success: " + e.successful.toString());

if (e.successful) {
//connection succeeded, so login
var lr:LoginRequest = new LoginRequest();
lr.userName = "Guest" + Math.round(1000 * Math.random()).toString();

_es.engine.send(lr);
}

}

private function onLoginResponse(e:LoginResponse):void {
print("Login accepted: " + e.successful.toString());
if (e.successful) {
joinRoom();

}

}

private function joinRoom():void
{
var crr:CreateRoomRequest = new CreateRoomRequest();
crr.roomName = "Test Room";
crr.zoneName = "Test Zone";

_es.engine.send(crr);
}

//This is executed every frame of the game
override public function update():void
{
}

override public function draw(g:Graphics, x:Number, y:Number):void
{
}

override public function handleCollision(event:Collision):void
{
}

//Leave this alone. Do your initializing inside init()
public function ConnectToES5(ignore:*, scene:GameState)
{
super(scene);
}
}
}

Disclaimer: All articles are geared towards Stencyl 3.0 and above. Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

7 Comments

snake13
Wait you did answer the question. I clearly was looking but not seeing.
0 3 weeks, 1 day ago
snake13
In the section where it says "How can I get a socket server is it free" you don't tell if it is free. Is it free.
0 3 weeks, 1 day ago
snake13
HELP! I STILL DONT GET HOW TO MAKE A MULTIPLAYER GAME!
-1 4 months ago
iggyvolz
Where exactly does this script go? I tried attatching it to my scene, but it gave to my console:

flixel v2.32 [release]
----------------------------------- ------------------------
Loading Scene 0
Start Fade In
Loading Scene: -1
Simple Test Scene
Error #1065
Could not load: electroserver
Could not initialize Script for Behavior: electroserver
FINISHED Entering...

0 1 year, 8 months ago
niccosw
Note that with the release of Stencyl 1.2, there is a much easier way to import SWC files.
0 2 years, 6 months ago
niccosw
Also: the "install a SWC file" link should point to http://www.stencyl.com/help/viewArt icle/84
0 2 years, 7 months ago
niccosw
Thanks for uploading my Stencylpedia article! However, I think the Level should be Expert, not Beginner.
0 2 years, 7 months ago

Sign In to Comment