SFS2X Docs / AdvancedTopics / using-udp-protocol
» Using the UDP protocol
SmartFoxServer 2X introduces support for the UDP protocol for all those client technologies that support it. This includes Unity3D and AIR 2.0 for Flash developers.
The UDP protocol is an unrealiable network protocol that does not guarantee delivery and ordering of the packets. If you want to learn more check this entry from Wikipedia. UDP can be useful when sending fast stream of packets to and from the server in real-time type games, typically for position/transformation updates etc...
Under the SFS 2X platform the developer can send any Extension call using UDP, although the default protocol used is TCP.
Even if technically the UDP protocol does not use a persistent connection (like in TCP) we usually refer to a Datagram socket (or channel) as a form of connection between the two endpoints. In order for a client to send and receive UDP data it will need to initialize his Datagram channel before any transmission is possible. This is done by calling the initUDP() method available on the client side.
» Getting started with the UDP protocol
In order to get started with UDP on SmartFoxServer 2X we first need to enable a UDP port on the server that will be used for the communication. By default SmartFoxServer 2X listens exclusively on localhost:9933 using the TCP protocol. You can launch the AdminTool, choose the Server Configuration module and add a new connector that listens for UDP data. An example would be adding a new socket listener for UDP port 9933.
On the client side you will just need to call the initUPD() method once and handle the response event. This can be done at any moment in your code after having logged in a Zone. In general we would highly recommend to do it exactly in your login handler.
var sfs:SmartFox = new SmartFox()
sfs.addEventListener(SFSEvent.CONNECTION, onConnection)
sfs.addEventListener(SFSEvent.LOGIN, onLogin)
sfs.addEventListener(SFSEvent.UDP_INIT, onUdpInit)
//... more initialization code here...
// Load the external cfg and start the connection
sfs.loadConfig()
private function onConnection(evt:SFSEvent):void
{
// Anonymous login
sfs.send(new LoginRequest())
}
private function onLogin(evt:SFSEvent):void
{
// Initialize UDP channel
sfs.initUDP(new AirUDPManager())
}
private function onUdpInit(evt:SFSEvent):void
{
if (evt.params.success)
{
trace("UDP ready!")
}
}
» Client side configuration
Before running the application make sure to review the settings in your sfs-config.xml file.In particular you should specify the address and port of the UDP connection so that it matches the server side configuration. Supposing you have set SFS2X to listen on 10.20.30.40:9933 for UDP, you should have the following in your config file:
<ip>10.20.30.40</ip> <port>9933</port> <zone>SimpleChat</zone> <udpIp>10.20.30.40</udpIp> <udpPort>9933</udpPort>
» UDP restrictions for Flash
As you can notice from the above snippet of code the initUDP method takes an argument:sfs.initUDP(new AirUDPManager())This actually applies exclusively to Flex/Air 2.0 while in C# you would simply use this code:
sfs.InitUDP();Adobe's support for UDP is in fact only available for standalone applications based on the AIR runtime (version 2.0 and higher) while no support is provided for Flash and Flex applications running in the browser.
In order to be able to support all the Adobe technologies with a single API package we introduced the AirUDPManager class which can be instantiated (exclusively when using the AIR SDK) and passed to the initUDP() method.



