forked from fpotter/socketio-cocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketio.m
113 lines (78 loc) · 2.9 KB
/
socketio.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// socketio.m
// SocketIoCocoa
//
// Created by Fred Potter on 11/11/10.
// Copyright 2010 Fred Potter. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SocketIoClient.h"
@interface SocketIoHandler : NSObject <SocketIoClientDelegate> {
NSString *_disconnectOnMessage;
}
@property (nonatomic, retain) NSString *disconnectOnMessage;
@end
@implementation SocketIoHandler
@synthesize disconnectOnMessage = _disconnectOnMessage;
- (void)socketIoClientDidConnect:(SocketIoClient *)client {
printf("SocketIO Connected.\n");
}
- (void)socketIoClientDidDisconnect:(SocketIoClient *)client {
printf("SocketIO Disconnected.\n");
}
- (void)socketIoClient:(SocketIoClient *)client didReceiveMessage:(NSString *)message isJSON:(BOOL)isJSON {
printf("SocketIO Received %s Message:\n%s\n", isJSON ? "JSON" : "TEXT", [message UTF8String]);
}
- (void)socketIoClient:(SocketIoClient *)client didSendMessage:(NSString *)message isJSON:(BOOL)isJSON {
printf("SocketIO Sent.\n");
if ([message isEqualToString:_disconnectOnMessage]) {
[client disconnect];
}
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *command = nil;
NSString *host = nil;
int port = 0;
if (argc > 1) {
host = [NSString stringWithUTF8String:argv[1]];
}
if (argc > 2) {
port = [[NSString stringWithUTF8String:argv[2]] intValue];
}
if (argc > 3) {
command = [NSString stringWithUTF8String:argv[3]];
}
if ([command isEqualToString:@"listen"]) {
printf("Listening on %s:%d...\n", [host UTF8String], port);
SocketIoClient *client = [[SocketIoClient alloc] initWithHost:host port:port];
SocketIoHandler *handler = [[SocketIoHandler alloc] init];
client.delegate = handler;
[client connect];
// Sleep forever
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
while (YES) {
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}
} else if ([command isEqualToString:@"send"]) {
NSString *message = [NSString stringWithUTF8String:argv[4]];
printf("Sending '%s' to %s:%d...\n", [message UTF8String], [host UTF8String], port);
SocketIoClient *client = [[SocketIoClient alloc] initWithHost:host port:port];
SocketIoHandler *handler = [[SocketIoHandler alloc] init];
[handler setDisconnectOnMessage:message];
client.delegate = handler;
[client connect];
[client send:message isJSON:NO];
// The message gets sent, then we just sleep forever...
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
while ((client.isConnected || client.isConnecting)) {
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
} else {
printf("usage: %s <host> <port> listen\n", argv[0]);
printf(" %s <host> <port> send <message>\n", argv[0]);
}
[pool drain];
return 0;
}