mirror of
https://github.com/Karmaz95/Snake_Apple.git
synced 2026-04-11 14:52:03 +02:00
Adding client_server NSNotification example
This commit is contained in:
15
X. NU/custom/mach_ipc/client_server_NSNotification/Makefile
Normal file
15
X. NU/custom/mach_ipc/client_server_NSNotification/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
CC = clang
|
||||
CFLAGS = -framework Foundation
|
||||
|
||||
all: client server
|
||||
|
||||
client: client.m
|
||||
$(CC) $(CFLAGS) client.m -o client
|
||||
|
||||
server: server.m
|
||||
$(CC) $(CFLAGS) server.m -o server
|
||||
|
||||
clean:
|
||||
rm -f client server *.o
|
||||
|
||||
.PHONY: all clean
|
||||
19
X. NU/custom/mach_ipc/client_server_NSNotification/client.m
Normal file
19
X. NU/custom/mach_ipc/client_server_NSNotification/client.m
Normal file
@@ -0,0 +1,19 @@
|
||||
// client.m
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@autoreleasepool {
|
||||
if (argc != 2) {
|
||||
NSLog(@"Usage: %s <message>", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
NSString *message = [NSString stringWithUTF8String:argv[1]];
|
||||
[[NSDistributedNotificationCenter defaultCenter]
|
||||
postNotificationName:@"com.crimson.message_service"
|
||||
object:nil
|
||||
userInfo:@{@"message": message}
|
||||
deliverImmediately:YES];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
31
X. NU/custom/mach_ipc/client_server_NSNotification/server.m
Normal file
31
X. NU/custom/mach_ipc/client_server_NSNotification/server.m
Normal file
@@ -0,0 +1,31 @@
|
||||
// server.m
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface MessageServer : NSObject
|
||||
- (void)handleMessage:(NSNotification *)notification;
|
||||
@end
|
||||
|
||||
@implementation MessageServer
|
||||
- (id)init {
|
||||
if (self = [super init]) {
|
||||
[[NSDistributedNotificationCenter defaultCenter]
|
||||
addObserver:self
|
||||
selector:@selector(handleMessage:)
|
||||
name:@"com.crimson.message_service"
|
||||
object:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)handleMessage:(NSNotification *)notification {
|
||||
NSLog(@"Received: %@", notification.userInfo[@"message"]);
|
||||
}
|
||||
@end
|
||||
|
||||
int main() {
|
||||
@autoreleasepool {
|
||||
MessageServer *server = [[MessageServer alloc] init];
|
||||
[[NSRunLoop currentRunLoop] run];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user