2015-05-14 16:28:09 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2015-10-07 15:28:34 +00:00
|
|
|
#import "RCTWebSocketModule.h"
|
2015-05-14 16:28:09 +00:00
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-06-12 18:05:01 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-05-14 16:28:09 +00:00
|
|
|
|
|
|
|
@implementation RCTSRWebSocket (React)
|
|
|
|
|
|
|
|
- (NSNumber *)reactTag
|
|
|
|
{
|
|
|
|
return objc_getAssociatedObject(self, _cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setReactTag:(NSNumber *)reactTag
|
|
|
|
{
|
|
|
|
objc_setAssociatedObject(self, @selector(reactTag), reactTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-10-07 15:28:34 +00:00
|
|
|
@implementation RCTWebSocketModule
|
2015-05-14 16:28:09 +00:00
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSNumber *, RCTSRWebSocket *> *_sockets;
|
2015-05-14 16:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
for (RCTSRWebSocket *socket in _sockets.allValues) {
|
2015-05-14 16:28:09 +00:00
|
|
|
socket.delegate = nil;
|
|
|
|
[socket close];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 14:19:05 +00:00
|
|
|
RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols options:(NSDictionary *)options socketID:(nonnull NSNumber *)socketID)
|
2015-05-14 16:28:09 +00:00
|
|
|
{
|
2016-03-15 14:19:05 +00:00
|
|
|
RCTSRWebSocket *webSocket = [[RCTSRWebSocket alloc] initWithURL:URL protocols:protocols options:options];
|
2015-05-14 16:28:09 +00:00
|
|
|
webSocket.delegate = self;
|
|
|
|
webSocket.reactTag = socketID;
|
2015-11-25 11:09:00 +00:00
|
|
|
if (!_sockets) {
|
|
|
|
_sockets = [NSMutableDictionary new];
|
|
|
|
}
|
2015-05-14 16:28:09 +00:00
|
|
|
_sockets[socketID] = webSocket;
|
|
|
|
[webSocket open];
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(send:(NSString *)message socketID:(nonnull NSNumber *)socketID)
|
2015-05-14 16:28:09 +00:00
|
|
|
{
|
|
|
|
[_sockets[socketID] send:message];
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(close:(nonnull NSNumber *)socketID)
|
2015-05-14 16:28:09 +00:00
|
|
|
{
|
|
|
|
[_sockets[socketID] close];
|
2015-11-14 18:25:00 +00:00
|
|
|
[_sockets removeObjectForKey:socketID];
|
2015-05-14 16:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTSRWebSocketDelegate methods
|
|
|
|
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
|
|
|
|
{
|
2015-12-22 17:22:01 +00:00
|
|
|
BOOL binary = [message isKindOfClass:[NSData class]];
|
2015-05-14 16:28:09 +00:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketMessage" body:@{
|
2015-12-22 17:22:01 +00:00
|
|
|
@"data": binary ? [message base64EncodedStringWithOptions:0] : message,
|
|
|
|
@"type": binary ? @"binary" : @"text",
|
2015-05-14 16:28:09 +00:00
|
|
|
@"id": webSocket.reactTag
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketOpen" body:@{
|
|
|
|
@"id": webSocket.reactTag
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketFailed" body:@{
|
2015-08-24 10:14:33 +00:00
|
|
|
@"message":error.localizedDescription,
|
2015-05-14 16:28:09 +00:00
|
|
|
@"id": webSocket.reactTag
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code
|
|
|
|
reason:(NSString *)reason wasClean:(BOOL)wasClean
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketClosed" body:@{
|
|
|
|
@"code": @(code),
|
2015-06-12 18:05:01 +00:00
|
|
|
@"reason": RCTNullIfNil(reason),
|
2015-05-14 16:28:09 +00:00
|
|
|
@"clean": @(wasClean),
|
|
|
|
@"id": webSocket.reactTag
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|