Initializes the UDP protocol by performing an handshake with the server.

Namespace: Sfs2X
Assembly: SmartFox2X (in SmartFox2X.dll) Version: 1.7.3.0 (1.7.3)

Syntax

C#
public void InitUDP(
	string udpHost,
	int udpPort
)

Parameters

udpHost
Type: System..::..String
The IP address of the server to connect to.
udpPort
Type: System..::..Int32
he UDP port to connect to.

Remarks

This method needs to be called only once. It can be executed at any moment provided that a connection to the server has already been established.
After a successful initialization, UDP requests can be sent to the server-side Extension at any moment.

If udpHost or udpPort arguments are not passed, the client will use the settings loaded via LoadConfig(String, Boolean) method.

UDP protocol is not available in case of websocket connection.

MTU note

The Maximum Transmission Unit (MTU), represents the largest amount of bytes that can be sent at once before packet fragmentation occurs. Since the UDP protocol uses a "nothing-or-all" approach to the transmission, it is important to keep in mind that, on average, a message size of 1100-1200 bytes is probably the maximum you can reach. If you exceed the MTU size the data will be "lost in hyperspace" (the Internet).

Another interesting matter is that there's no fixed size for the MTU, each operating system uses a slighlty different size. Because of this we suggest a conservative data size of 1000-1200 bytes per packet to avoid packet loss.

The SFS2X protocol compression allows to send 2-3KBytes of uncompressed data which usually is squeezed down to a size of ~1000 bytes. If you have larger data to send we suggest to organize it in smaller chunks so that they don't exceed the suggested MTU size.

More details about the MTU can be found here: http://en.wikipedia.org/wiki/Maximum_transmission_unit.

Examples

The following example initializes the UDP communication, sends a request to an Extension and handles the related events:
 Copy imageCopy
void SomeMethod() {
    sfs.AddEventListener(SFSEvent.UPD_INIT, OnUDPInit);
    sfs.InitUDP();
}
void OnUDPInit(BaseEvent evt) {
    if ((bool)evt.Params["success"]) {
            // Execute an extension call via UDP
        sfs.Send( new ExtensionRequest("udpTest", new SFSObject(), null, true) ):
        } else {
        Console.WriteLine("UDP init failed!");                          // .Net / Unity
        System.Diagnostics.Debug.WriteLine("UDP init failed!");         // Windows Universal
    }
}

See Also