| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #import "ARDWebSocketChannel.h" | |
| 29 | |
| 30 #import "ARDLogging.h" | |
| 31 #import "ARDUtilities.h" | |
| 32 #import "SRWebSocket.h" | |
| 33 | |
| 34 // TODO(tkchin): move these to a configuration object. | |
| 35 static NSString const *kARDWSSMessageErrorKey = @"error"; | |
| 36 static NSString const *kARDWSSMessagePayloadKey = @"msg"; | |
| 37 | |
| 38 @interface ARDWebSocketChannel () <SRWebSocketDelegate> | |
| 39 @end | |
| 40 | |
| 41 @implementation ARDWebSocketChannel { | |
| 42 NSURL *_url; | |
| 43 NSURL *_restURL; | |
| 44 SRWebSocket *_socket; | |
| 45 } | |
| 46 | |
| 47 @synthesize delegate = _delegate; | |
| 48 @synthesize state = _state; | |
| 49 @synthesize roomId = _roomId; | |
| 50 @synthesize clientId = _clientId; | |
| 51 | |
| 52 - (instancetype)initWithURL:(NSURL *)url | |
| 53 restURL:(NSURL *)restURL | |
| 54 delegate:(id<ARDSignalingChannelDelegate>)delegate { | |
| 55 if (self = [super init]) { | |
| 56 _url = url; | |
| 57 _restURL = restURL; | |
| 58 _delegate = delegate; | |
| 59 _socket = [[SRWebSocket alloc] initWithURL:url]; | |
| 60 _socket.delegate = self; | |
| 61 ARDLog(@"Opening WebSocket."); | |
| 62 [_socket open]; | |
| 63 } | |
| 64 return self; | |
| 65 } | |
| 66 | |
| 67 - (void)dealloc { | |
| 68 [self disconnect]; | |
| 69 } | |
| 70 | |
| 71 - (void)setState:(ARDSignalingChannelState)state { | |
| 72 if (_state == state) { | |
| 73 return; | |
| 74 } | |
| 75 _state = state; | |
| 76 [_delegate channel:self didChangeState:_state]; | |
| 77 } | |
| 78 | |
| 79 - (void)registerForRoomId:(NSString *)roomId | |
| 80 clientId:(NSString *)clientId { | |
| 81 NSParameterAssert(roomId.length); | |
| 82 NSParameterAssert(clientId.length); | |
| 83 _roomId = roomId; | |
| 84 _clientId = clientId; | |
| 85 if (_state == kARDSignalingChannelStateOpen) { | |
| 86 [self registerWithCollider]; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 - (void)sendMessage:(ARDSignalingMessage *)message { | |
| 91 NSParameterAssert(_clientId.length); | |
| 92 NSParameterAssert(_roomId.length); | |
| 93 NSData *data = [message JSONData]; | |
| 94 if (_state == kARDSignalingChannelStateRegistered) { | |
| 95 NSString *payload = | |
| 96 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
| 97 NSDictionary *message = @{ | |
| 98 @"cmd": @"send", | |
| 99 @"msg": payload, | |
| 100 }; | |
| 101 NSData *messageJSONObject = | |
| 102 [NSJSONSerialization dataWithJSONObject:message | |
| 103 options:NSJSONWritingPrettyPrinted | |
| 104 error:nil]; | |
| 105 NSString *messageString = | |
| 106 [[NSString alloc] initWithData:messageJSONObject | |
| 107 encoding:NSUTF8StringEncoding]; | |
| 108 ARDLog(@"C->WSS: %@", messageString); | |
| 109 [_socket send:messageString]; | |
| 110 } else { | |
| 111 NSString *dataString = | |
| 112 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
| 113 ARDLog(@"C->WSS POST: %@", dataString); | |
| 114 NSString *urlString = | |
| 115 [NSString stringWithFormat:@"%@/%@/%@", | |
| 116 [_restURL absoluteString], _roomId, _clientId]; | |
| 117 NSURL *url = [NSURL URLWithString:urlString]; | |
| 118 [NSURLConnection sendAsyncPostToURL:url | |
| 119 withData:data | |
| 120 completionHandler:nil]; | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 - (void)disconnect { | |
| 125 if (_state == kARDSignalingChannelStateClosed || | |
| 126 _state == kARDSignalingChannelStateError) { | |
| 127 return; | |
| 128 } | |
| 129 [_socket close]; | |
| 130 ARDLog(@"C->WSS DELETE rid:%@ cid:%@", _roomId, _clientId); | |
| 131 NSString *urlString = | |
| 132 [NSString stringWithFormat:@"%@/%@/%@", | |
| 133 [_restURL absoluteString], _roomId, _clientId]; | |
| 134 NSURL *url = [NSURL URLWithString:urlString]; | |
| 135 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
| 136 request.HTTPMethod = @"DELETE"; | |
| 137 request.HTTPBody = nil; | |
| 138 [NSURLConnection sendAsyncRequest:request completionHandler:nil]; | |
| 139 } | |
| 140 | |
| 141 #pragma mark - SRWebSocketDelegate | |
| 142 | |
| 143 - (void)webSocketDidOpen:(SRWebSocket *)webSocket { | |
| 144 ARDLog(@"WebSocket connection opened."); | |
| 145 self.state = kARDSignalingChannelStateOpen; | |
| 146 if (_roomId.length && _clientId.length) { | |
| 147 [self registerWithCollider]; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { | |
| 152 NSString *messageString = message; | |
| 153 NSData *messageData = [messageString dataUsingEncoding:NSUTF8StringEncoding]; | |
| 154 id jsonObject = [NSJSONSerialization JSONObjectWithData:messageData | |
| 155 options:0 | |
| 156 error:nil]; | |
| 157 if (![jsonObject isKindOfClass:[NSDictionary class]]) { | |
| 158 ARDLog(@"Unexpected message: %@", jsonObject); | |
| 159 return; | |
| 160 } | |
| 161 NSDictionary *wssMessage = jsonObject; | |
| 162 NSString *errorString = wssMessage[kARDWSSMessageErrorKey]; | |
| 163 if (errorString.length) { | |
| 164 ARDLog(@"WSS error: %@", errorString); | |
| 165 return; | |
| 166 } | |
| 167 NSString *payload = wssMessage[kARDWSSMessagePayloadKey]; | |
| 168 ARDSignalingMessage *signalingMessage = | |
| 169 [ARDSignalingMessage messageFromJSONString:payload]; | |
| 170 ARDLog(@"WSS->C: %@", payload); | |
| 171 [_delegate channel:self didReceiveMessage:signalingMessage]; | |
| 172 } | |
| 173 | |
| 174 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { | |
| 175 ARDLog(@"WebSocket error: %@", error); | |
| 176 self.state = kARDSignalingChannelStateError; | |
| 177 } | |
| 178 | |
| 179 - (void)webSocket:(SRWebSocket *)webSocket | |
| 180 didCloseWithCode:(NSInteger)code | |
| 181 reason:(NSString *)reason | |
| 182 wasClean:(BOOL)wasClean { | |
| 183 ARDLog(@"WebSocket closed with code: %ld reason:%@ wasClean:%d", | |
| 184 (long)code, reason, wasClean); | |
| 185 NSParameterAssert(_state != kARDSignalingChannelStateError); | |
| 186 self.state = kARDSignalingChannelStateClosed; | |
| 187 } | |
| 188 | |
| 189 #pragma mark - Private | |
| 190 | |
| 191 - (void)registerWithCollider { | |
| 192 if (_state == kARDSignalingChannelStateRegistered) { | |
| 193 return; | |
| 194 } | |
| 195 NSParameterAssert(_roomId.length); | |
| 196 NSParameterAssert(_clientId.length); | |
| 197 NSDictionary *registerMessage = @{ | |
| 198 @"cmd": @"register", | |
| 199 @"roomid" : _roomId, | |
| 200 @"clientid" : _clientId, | |
| 201 }; | |
| 202 NSData *message = | |
| 203 [NSJSONSerialization dataWithJSONObject:registerMessage | |
| 204 options:NSJSONWritingPrettyPrinted | |
| 205 error:nil]; | |
| 206 NSString *messageString = | |
| 207 [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding]; | |
| 208 ARDLog(@"Registering on WSS for rid:%@ cid:%@", _roomId, _clientId); | |
| 209 // Registration can fail if server rejects it. For example, if the room is | |
| 210 // full. | |
| 211 [_socket send:messageString]; | |
| 212 self.state = kARDSignalingChannelStateRegistered; | |
| 213 } | |
| 214 | |
| 215 @end | |
| OLD | NEW |