This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 439
/
SocketIOTransportWebsocket.m
131 lines (109 loc) · 3.14 KB
/
SocketIOTransportWebsocket.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// SocketIOTransportWebsocket.m
// v0.5.1
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
// by Fred Potter <fpotter@pieceable.com>
//
// using
// https://github.com/square/SocketRocket
//
// reusing some parts of
// /socket.io/socket.io.js
//
// Created by Philipp Kyeck http://beta-interactive.de
//
// With help from
// https://github.com/pkyeck/socket.IO-objc/blob/master/CONTRIBUTORS.md
//
#import "SocketIOTransportWebsocket.h"
#import "SocketIO.h"
#define DEBUG_LOGS 0
#if DEBUG_LOGS
#define DEBUGLOG(...) NSLog(__VA_ARGS__)
#else
#define DEBUGLOG(...)
#endif
static NSString* kInsecureSocketURL = @"ws://%@/socket.io/1/websocket/%@";
static NSString* kSecureSocketURL = @"wss://%@/socket.io/1/websocket/%@";
static NSString* kInsecureSocketPortURL = @"ws://%@:%d/socket.io/1/websocket/%@";
static NSString* kSecureSocketPortURL = @"wss://%@:%d/socket.io/1/websocket/%@";
@implementation SocketIOTransportWebsocket
@synthesize delegate;
- (id) initWithDelegate:(id<SocketIOTransportDelegate>)delegate_
{
self = [super init];
if (self) {
self.delegate = delegate_;
}
return self;
}
- (BOOL) isReady
{
return _webSocket.readyState == SR_OPEN;
}
- (void) open
{
NSString *urlStr;
NSString *format;
if (delegate.port) {
format = delegate.useSecure ? kSecureSocketPortURL : kInsecureSocketPortURL;
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.port, delegate.sid];
}
else {
format = delegate.useSecure ? kSecureSocketURL : kInsecureSocketURL;
urlStr = [NSString stringWithFormat:format, delegate.host, delegate.sid];
}
NSURL *url = [NSURL URLWithString:urlStr];
_webSocket = nil;
_webSocket = [[SRWebSocket alloc] initWithURL:url];
_webSocket.delegate = self;
DEBUGLOG(@"Opening %@", url);
[_webSocket open];
}
- (void) dealloc
{
[_webSocket setDelegate:nil];
}
- (void) close
{
[_webSocket close];
}
- (void) send:(NSString*)request
{
[_webSocket send:request];
}
# pragma mark -
# pragma mark WebSocket Delegate Methods
- (void) webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
if([delegate respondsToSelector:@selector(onData:)]) {
[delegate onData:message];
}
}
- (void) webSocketDidOpen:(SRWebSocket *)webSocket
{
DEBUGLOG(@"Socket opened.");
}
- (void) webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
DEBUGLOG(@"Socket failed with error ... %@", [error localizedDescription]);
// Assuming this resulted in a disconnect
if([delegate respondsToSelector:@selector(onDisconnect:)]) {
[delegate onDisconnect:error];
}
}
- (void) webSocket:(SRWebSocket *)webSocket
didCloseWithCode:(NSInteger)code
reason:(NSString *)reason
wasClean:(BOOL)wasClean
{
DEBUGLOG(@"Socket closed. %@", reason);
if([delegate respondsToSelector:@selector(onDisconnect:)]) {
[delegate onDisconnect:[NSError errorWithDomain:SocketIOError
code:SocketIOWebSocketClosed
userInfo:nil]];
}
}
@end