ClientTOpen(OpenParams, NotifyDataChangedEventHandler, CallbackContext) Method |
Opens a client connection, calling dataChangedHandler for each message received.
Namespace: Demo3D.NetAssembly: Demo3D.IO (in Demo3D.IO.dll) Version: 19.00.00
Syntaxpublic static Client<T> Open(
OpenParams parameters,
NotifyDataChangedEventHandler dataChangedHandler,
CallbackContext callbackContext
)
Parameters
- parameters OpenParams
- Parameters to open the client connection.
- dataChangedHandler NotifyDataChangedEventHandler
- Data received handler.
- callbackContext CallbackContext
- Defines the context in which the callback should be called.
Return Value
ClientTThe 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.
Exampleusing 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;
[Auto] SimplePropertyValue<int> tcpPort;
[Auto] IEnumerable OnInitialize(Visual sender) {
yield return Connect();
}
async Task Connect() {
var address = new ProtocolAddressBuilder(TCP.Scheme, tcpAddress, tcpPort).Address;
await Client<IPacketIOService>.OpenAsync(sync: false, address, ReceiveMessage);
}
void ReceiveMessage(ProtocolSocket socket, object service, NotifyDataChangedEventArgs e) {
try {
var args = (PacketChangedEventArgs)e;
var message = args.GetData();
HandleMessage(message);
}
catch (Exception x) {
socket.Close(x);
}
}
void HandleMessage(in BufferSegment message) {
print("Message received: " + message.Length + " bytes");
}
}
}
See Also