forked from francoisp/socket.IO-objc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.m
110 lines (83 loc) · 3.03 KB
/
ViewController.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
//
// ViewController.m
// SocketTesterARC
//
// Created by Kyeck Philipp on 01.06.12.
// Copyright (c) 2012 beta_interactive. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad
{
[super viewDidLoad];
// create socket.io client instance
socketIO = [[SocketIO alloc] initWithDelegate:self];
// you can update the resource name of the handshake URL
// see https://github.com/pkyeck/socket.IO-objc/pull/80
// [socketIO setResourceName:@"whatever"];
// if you want to use https instead of http
// socketIO.useSecure = YES;
// pass cookie(s) to handshake endpoint (e.g. for auth)
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
@"localhost", NSHTTPCookieDomain,
@"/", NSHTTPCookiePath,
@"auth", NSHTTPCookieName,
@"56cdea636acdf132", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
NSArray *cookies = [NSArray arrayWithObjects:cookie, nil];
socketIO.cookies = cookies;
// connect to the socket.io server that is running locally at port 3000
[socketIO connectToHost:@"localhost" onPort:3000];
}
# pragma mark -
# pragma mark socket.IO-objc delegate methods
- (void) socketIODidConnect:(SocketIO *)socket
{
NSLog(@"socket.io connected.");
}
- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet
{
NSLog(@"didReceiveEvent()");
// test acknowledge
SocketIOCallback cb = ^(id argsData) {
NSDictionary *response = argsData;
// do something with response
NSLog(@"ack arrived: %@", response);
// test forced disconnect
[socketIO disconnectForced];
};
[socketIO sendMessage:@"hello back!" withAcknowledge:cb];
// test different event data types
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"test1" forKey:@"key1"];
[dict setObject:@"test2" forKey:@"key2"];
[socketIO sendEvent:@"welcome" withData:dict];
[socketIO sendEvent:@"welcome" withData:@"testWithString"];
NSArray *arr = [NSArray arrayWithObjects:@"test1", @"test2", nil];
[socketIO sendEvent:@"welcome" withData:arr];
}
- (void) socketIO:(SocketIO *)socket onError:(NSError *)error
{
if ([error code] == SocketIOUnauthorized) {
NSLog(@"not authorized");
} else {
NSLog(@"onError() %@", error);
}
}
- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error
{
NSLog(@"socket.io disconnected. did error occur? %@", error);
}
# pragma mark -
- (void) viewDidUnload
{
[super viewDidUnload];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end