Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: talk/examples/objc/AppRTCDemo/ARDWebSocketChannel.m

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

Powered by Google App Engine
This is Rietveld 408576698