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 Unity, Adobe AIR 2.0 for Flash developers and many more.
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 SFS2X 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 you first need to enable a UDP port on the server that will be used for the communication. By default SFS2X listens exclusively on localhost:9933 using the TCP protocol. You can launch the AdminTool, choose the Server Configurator module and add a new connector that listens for UDP data. As an example you would be adding a new socket listener for UDP over 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. Also you should make sure that the client configuration contains the address and port of the UDP connection matching the server side configuration.
// Initialize SFS2X client SmartFox sfs = new SmartFox(); // Add event listeners sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection); sfs.AddEventListener(SFSEvent.LOGIN, OnLogin); sfs.AddEventListener(SFSEvent.UDP_INIT, OnUdpInit); ... // Set connection parameters ConfigData cfg = new ConfigData(); cfg.Host = "127.0.0.1"; cfg.Port = 9933; cfg.Zone = "Basic Examples"; cfg.UdpHost = "127.0.0.1"; cfg.UdpPort = 9933; // Connect to SFS2X sfs.Connect(cfg);
private void OnConnection(BaseEvent evt) { // Anonymous login sfs.Send(new Sfs2x.Requests.LoginRequest()); } private void OnLogin(BaseEvent evt) { // Initialize UDP channel sfs.InitUDP(); } private void OnUdpInit(BaseEvent evt) { if ((bool)evt.Params["success"]) Console.WriteLine("UDP ready!"); }
// Initialize SFS2X client var sfs:SmartFox = new SmartFox(); sfs.addEventListener(SFSEvent.CONNECTION, onConnection); sfs.addEventListener(SFSEvent.LOGIN, onLogin); sfs.addEventListener(SFSEvent.UDP_INIT, onUdpInit); ... // Set connection parameters var cfg:ConfigData = new ConfigData(); cfg.host = "127.0.0.1"; cfg.port = 9933; cfg.zone = "Basic Examples"; cfg.udpHost = "127.0.0.1"; cfg.udpPort = 9933; // Connect to SFS2X sfs.connectWithConfig(cfg);
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!"); } }
» 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 Adobe Flex/Air 2.0, while in C# and other clients supporting UDP 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.