ClientTOpenAsync(Boolean, ProtocolAddress, 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 Task<Client<T>> OpenAsync(
bool sync,
ProtocolAddress address,
NotifyDataChangedEventHandler dataChangedHandler,
CallbackContext callbackContext = CallbackContext.CallerContext
)
Parameters
- sync Boolean
- If true, the Task returned is guaranteed to be complete.
- 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
TaskClientTThe 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