Click or drag to resize

ClientTOpen(ProtocolAddress, NotifyDataChangedEventHandler, CallbackContext) Method

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

Namespace: Demo3D.Net
Assembly: Demo3D.IO (in Demo3D.IO.dll) Version: 19.00.00
Syntax
C#
public static Client<T> Open(
	ProtocolAddress address,
	NotifyDataChangedEventHandler dataChangedHandler,
	CallbackContext callbackContext = CallbackContext.CallerContext
)

Parameters

address  ProtocolAddress
Address to connect to.
dataChangedHandler  NotifyDataChangedEventHandler
Data received handler.
callbackContext  CallbackContext  (Optional)
Defines the context in which the callback should be called.

Return Value

ClientT
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.
Example
C#
using System;
using System.Collections;
using System.Threading.Tasks;
using Demo3D.IO;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

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(in BufferSegment message) {
            print("Message received: " + message.Length + " bytes");
        }
    }
}
See Also