Click or drag to resize

ClientTOpenAsync Method (Boolean, OpenParams, NotifyDataChangedEventHandler, CallbackContext)

Opens a client connection, calling dataChangedHandler for each message received.

Namespace:  Demo3D.Net
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 15.0.2.11458
Syntax
C#
public static Task<Client<T>> OpenAsync(
	bool sync,
	OpenParams parameters,
	NotifyDataChangedEventHandler dataChangedHandler,
	CallbackContext callbackContext = CallbackContext.CallerContext
)

Parameters

sync
Type: SystemBoolean
If true, the Task returned is guaranteed to be complete.
parameters
Type: Demo3D.NetOpenParams
Parameters to open the client connection.
dataChangedHandler
Type: Demo3D.NetNotifyDataChangedEventHandler
Data received handler.
callbackContext (Optional)
Type: Demo3D.NetCallbackContext
Defines the context in which the callback should be called.

Return Value

Type: TaskClientT
The new client.
Remarks
If callbackContext is CallerContext then dataChangedHandler is Demo3D thread safe. It's called in the same thread (synchronization context) as the caller to this method.
Examples
C#
using System;
using System.Collections;
using System.Threading.Tasks;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

using Buffer = Demo3D.IO.Buffer;

namespace Examples.Net.TcpClientDataNotifierExample {
    [Auto] public class Client {
        [Auto] IBuilder                    app;
        [Auto] PrintDelegate               print;
        [Auto] SimplePropertyValue<string> tcpAddress;  // The hostname to connect to.
        [Auto] SimplePropertyValue<int>    tcpPort;     // The port to connect to.

        [Auto] IEnumerable OnInitialize(Visual sender) {
            yield return Connect();
        }

        async Task Connect() {
            // Build a TCP address from the host and port.
            // A TCP address will look something like "tcp://host:1234".
            var address = new ProtocolAddressBuilder(TCP.Scheme, tcpAddress, tcpPort).Address;

            // Connect to the server using the configured tcpAddress and tcpPort.
            // Calls ReceiveMessage for every message received.
            await Client<IPacketIOService>.OpenAsync(sync: false, address, ReceiveMessage);
        }

        void ReceiveMessage(ProtocolSocket socket, object service, NotifyDataChangedEventArgs e) {
            try {
                var args    = (PacketChangedEventArgs)e;  // A TCP client uses PacketChangedEventArgs.
                var message = args.GetData();             // Read from the packet.

                // Handle the message.
                HandleMessage(message);
            }
            catch (Exception x) {
                socket.Close(x);
            }
        }

        void HandleMessage(Buffer message) {
            print("Message received: " + message.Length + " bytes");
        }
    }
}
See Also