Welcome to the documentation for YLTCPBroadcaster – a simple utility library for sending information between devices using TCP/IP sockets.
Installation
To install YLTCPBroadcaster, you can use CocoaPods. Add the following line to your Podfile:
pod 'YLTCPBroadcaster'
Then, run the following command:
pod install
Getting Started
To start using YLTCPBroadcaster in your project, you need to import the library:
#import <YLTCPBroadcaster/YLTCPBroadcaster.h>
Once imported, you can create an instance of YLTCPBroadcaster and start broadcasting messages:
YLTCPBroadcaster *broadcaster = [[YLTCPBroadcaster alloc] init]; [broadcaster startBroadcastingWithPort:12345];
The above code will start a TCP server that listens to connections on port 12345. You can change the port number as per your requirements.
Methods
YLTCPBroadcaster provides the following methods:
- startBroadcastingWithPort:
- stopBroadcasting
- sendDataToAllClients:
- sendData:toClientWithAddress:
- delegate
Delegation
YLTCPBroadcaster supports delegation to receive updates and handle events. To register a delegate, use the delegate
property:
broadcaster.delegate = self;
Your class should conform to the YLTCPBroadcasterDelegate
protocol and implement the following methods:
// Delegate method called when a new client is connected - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didConnectToClientWithAddress:(NSString *)address; // Delegate method called when a client is disconnected - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didDisconnectFromClientWithAddress:(NSString *)address; // Delegate method called when data is received from a client - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didReceiveData:(NSData *)data fromClientWithAddress:(NSString *)address;
Stopping Broadcasting
To stop the broadcasting, use the stopBroadcasting
method:
[broadcaster stopBroadcasting];
Example
Here’s an example code snippet that demonstrates how to use YLTCPBroadcaster:
#import <YLTCPBroadcaster/YLTCPBroadcaster.h> @interface MyViewController () <YLTCPBroadcasterDelegate> @end @implementation MyViewController { YLTCPBroadcaster *_broadcaster; } - (void)viewDidLoad { [super viewDidLoad]; // Create an instance of YLTCPBroadcaster _broadcaster = [[YLTCPBroadcaster alloc] init]; _broadcaster.delegate = self; // Start broadcasting on port 12345 [_broadcaster startBroadcastingWithPort:12345]; } // Delegate methods - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didConnectToClientWithAddress:(NSString *)address { NSLog(@"Connected to client at %@", address); } - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didDisconnectFromClientWithAddress:(NSString *)address { NSLog(@"Disconnected from client at %@", address); } - (void)broadcaster:(YLTCPBroadcaster *)broadcaster didReceiveData:(NSData *)data fromClientWithAddress:(NSString *)address { // Process received data NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Received data from %@: %@", address, receivedString); } - (void)dealloc { // Stop broadcasting [_broadcaster stopBroadcasting]; } @end