|
14 | 14 |
|
15 | 15 | ## Non-ARC version
|
16 | 16 |
|
17 |
| - If you're old school - there's still the [non-ARC version](https://github.com/pkyeck/socket.IO-objc/tree/non-arc) for you. |
18 |
| - This version (the non-ARC one) is out-of-date and won't be maintained any further (at least not by me). |
| 17 | +If you're old school - there's still the [non-ARC version](https://github.com/pkyeck/socket.IO-objc/tree/non-arc) for you. |
| 18 | +This version (the non-ARC one) is out-of-date and won't be maintained any further (at least not by me). |
19 | 19 |
|
20 | 20 | ## Usage
|
21 | 21 |
|
22 |
| - The easiest way to connect to your Socket.IO / node.js server is |
23 |
| - |
24 |
| - ``` objective-c |
25 |
| - SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self]; |
26 |
| - [socketIO connectToHost:@"localhost" onPort:3000]; |
27 |
| - ``` |
28 |
| - |
29 |
| - If required, additional parameters can be included in the handshake by adding an `NSDictionary` to the `withParams` option: |
30 |
| - |
31 |
| - ``` objective-c |
32 |
| - [socketIO connectToHost:@"localhost" |
33 |
| - onPort:3000 |
34 |
| - withParams:[NSDictionary dictionaryWithObjectsAndKeys:@"1234", @"auth_token", nil] |
35 |
| - ]; |
36 |
| - ``` |
37 |
| - |
38 |
| - A namespace can also be defined in the connection details: |
39 |
| - |
40 |
| - ``` objective-c |
41 |
| - [socketIO connectToHost:@"localhost" onPort:3000 withParams:nil withNamespace:@"/users"]; |
42 |
| - ``` |
43 |
| - |
44 |
| - There are different methods to send data to the server |
45 |
| - |
46 |
| - ``` objective-c |
47 |
| - - (void) sendMessage:(NSString *)data; |
48 |
| - - (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function; |
49 |
| - - (void) sendJSON:(NSDictionary *)data; |
50 |
| - - (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function; |
51 |
| - - (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data; |
52 |
| - - (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknowledge:(SocketIOCallback)function; |
53 |
| - ``` |
54 |
| - |
55 |
| - So you could send a normal Message like this |
56 |
| - |
57 |
| - ``` objective-c |
58 |
| - [socketIO sendMessage:@"hello world"]; |
59 |
| - ``` |
60 |
| - |
61 |
| - or an Event (including some data) like this |
62 |
| - |
63 |
| - ``` objective-c |
64 |
| - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; |
65 |
| - [dict setObject:@"test1" forKey:@"key1"]; |
66 |
| - [dict setObject:@"test2" forKey:@"key2"]; |
67 |
| - |
68 |
| - [socketIO sendEvent:@"welcome" withData:dict]; |
69 |
| - ``` |
70 |
| - |
71 |
| - If you want the server to acknowledge your Message/Event you would also pass a SocketIOCallback block |
72 |
| - |
73 |
| - ``` objective-c |
74 |
| - SocketIOCallback cb = ^(id argsData) { |
75 |
| - NSDictionary *response = argsData; |
76 |
| - // do something with response |
77 |
| - }; |
78 |
| - [socketIO sendEvent:@"welcomeAck" withData:dict andAcknowledge:cb]; |
79 |
| - ``` |
80 |
| - |
81 |
| - All delegate methods are optional - you could implement the following |
82 |
| - |
83 |
| - ``` objective-c |
84 |
| - - (void) socketIODidConnect:(SocketIO *)socket; |
85 |
| - - (void) socketIODidDisconnect:(SocketIO *)socket; // deprecated |
86 |
| - - (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error; |
87 |
| - - (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet; |
88 |
| - - (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet; |
89 |
| - - (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet; |
90 |
| - - (void) socketIO:(SocketIO *)socket didSendMessage:(SocketIOPacket *)packet; |
91 |
| - - (void) socketIO:(SocketIO *)socket onError:(NSError *)error; |
92 |
| - ``` |
93 |
| - |
94 |
| - These two callbacks are deprecated - please don't use them anymore - they will |
95 |
| - be removed in upcoming releases: |
96 |
| - |
97 |
| - ``` objective-c |
98 |
| - - (void) socketIOHandshakeFailed:(SocketIO *)socket; |
99 |
| - - (void) socketIO:(SocketIO *)socket failedToConnectWithError:(NSError *)error; |
100 |
| - ``` |
101 |
| - |
102 |
| - To process an incoming Message just |
103 |
| - |
104 |
| - ``` objective-c |
105 |
| - - (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet |
106 |
| - { |
107 |
| - NSLog(@"didReceiveMessage() >>> data: %@", packet.data); |
108 |
| - } |
109 |
| - ``` |
| 22 | +The easiest way to connect to your Socket.IO / node.js server is |
| 23 | + |
| 24 | +``` objective-c |
| 25 | +SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self]; |
| 26 | +[socketIO connectToHost:@"localhost" onPort:3000]; |
| 27 | +``` |
| 28 | +
|
| 29 | +If required, additional parameters can be included in the handshake by adding an `NSDictionary` to the `withParams` option: |
| 30 | +
|
| 31 | +``` objective-c |
| 32 | +[socketIO connectToHost:@"localhost" |
| 33 | + onPort:3000 |
| 34 | + withParams:[NSDictionary dictionaryWithObjectsAndKeys:@"1234", @"auth_token", nil] |
| 35 | +]; |
| 36 | +``` |
| 37 | + |
| 38 | +A namespace can also be defined in the connection details: |
| 39 | + |
| 40 | +``` objective-c |
| 41 | +[socketIO connectToHost:@"localhost" onPort:3000 withParams:nil withNamespace:@"/users"]; |
| 42 | +``` |
| 43 | +
|
| 44 | +There are different methods to send data to the server |
| 45 | +
|
| 46 | +``` objective-c |
| 47 | +- (void) sendMessage:(NSString *)data; |
| 48 | +- (void) sendMessage:(NSString *)data withAcknowledge:(SocketIOCallback)function; |
| 49 | +- (void) sendJSON:(NSDictionary *)data; |
| 50 | +- (void) sendJSON:(NSDictionary *)data withAcknowledge:(SocketIOCallback)function; |
| 51 | +- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data; |
| 52 | +- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknowledge:(SocketIOCallback)function; |
| 53 | +``` |
| 54 | + |
| 55 | +So you could send a normal Message like this |
| 56 | + |
| 57 | +``` objective-c |
| 58 | +[socketIO sendMessage:@"hello world"]; |
| 59 | +``` |
| 60 | +
|
| 61 | +or an Event (including some data) like this |
| 62 | +
|
| 63 | +``` objective-c |
| 64 | +NSMutableDictionary *dict = [NSMutableDictionary dictionary]; |
| 65 | +[dict setObject:@"test1" forKey:@"key1"]; |
| 66 | +[dict setObject:@"test2" forKey:@"key2"]; |
| 67 | +
|
| 68 | +[socketIO sendEvent:@"welcome" withData:dict]; |
| 69 | +``` |
| 70 | + |
| 71 | +If you want the server to acknowledge your Message/Event you would also pass a SocketIOCallback block |
| 72 | + |
| 73 | +``` objective-c |
| 74 | +SocketIOCallback cb = ^(id argsData) { |
| 75 | + NSDictionary *response = argsData; |
| 76 | + // do something with response |
| 77 | +}; |
| 78 | +[socketIO sendEvent:@"welcomeAck" withData:dict andAcknowledge:cb]; |
| 79 | +``` |
| 80 | +
|
| 81 | +All delegate methods are optional - you could implement the following |
| 82 | +
|
| 83 | +``` objective-c |
| 84 | +- (void) socketIODidConnect:(SocketIO *)socket; |
| 85 | +- (void) socketIODidDisconnect:(SocketIO *)socket; // deprecated |
| 86 | +- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error; |
| 87 | +- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet; |
| 88 | +- (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet; |
| 89 | +- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet; |
| 90 | +- (void) socketIO:(SocketIO *)socket didSendMessage:(SocketIOPacket *)packet; |
| 91 | +- (void) socketIO:(SocketIO *)socket onError:(NSError *)error; |
| 92 | +``` |
| 93 | + |
| 94 | +These two callbacks are deprecated - please don't use them anymore - they will |
| 95 | +be removed in upcoming releases: |
| 96 | + |
| 97 | +``` objective-c |
| 98 | +- (void) socketIOHandshakeFailed:(SocketIO *)socket; |
| 99 | +- (void) socketIO:(SocketIO *)socket failedToConnectWithError:(NSError *)error; |
| 100 | +``` |
| 101 | + |
| 102 | +To process an incoming Message just |
| 103 | + |
| 104 | +``` objective-c |
| 105 | +- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet |
| 106 | +{ |
| 107 | + NSLog(@"didReceiveMessage() >>> data: %@", packet.data); |
| 108 | +} |
| 109 | +``` |
110 | 110 |
|
111 | 111 | ## Authors
|
112 | 112 |
|
|
0 commit comments