diff --git a/Libraries/WebSocket/RCTWebSocketModule.m b/Libraries/WebSocket/RCTWebSocketModule.m index 12f6da8a0..6ccf3f495 100644 --- a/Libraries/WebSocket/RCTWebSocketModule.m +++ b/Libraries/WebSocket/RCTWebSocketModule.m @@ -61,7 +61,7 @@ RCT_EXPORT_MODULE() } } -RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols headers:(NSDictionary *)headers socketID:(nonnull NSNumber *)socketID) +RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols options:(NSDictionary *)options socketID:(nonnull NSNumber *)socketID) { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; @@ -78,7 +78,7 @@ RCT_EXPORT_METHOD(connect:(NSURL *)URL protocols:(NSArray *)protocols headers:(N request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; // Load supplied headers - [headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { + [options[@"headers"] enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { [request addValue:[RCTConvert NSString:value] forHTTPHeaderField:key]; }]; diff --git a/Libraries/WebSocket/WebSocket.js b/Libraries/WebSocket/WebSocket.js index 83896e969..96f159a51 100644 --- a/Libraries/WebSocket/WebSocket.js +++ b/Libraries/WebSocket/WebSocket.js @@ -93,12 +93,27 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { // `WebSocket.isAvailable` will return `false`, and WebSocket constructor will throw an error static isAvailable: boolean = !!WebSocketModule; - constructor(url: string, protocols: ?string | ?Array, options: ?{origin?: string}) { + constructor(url: string, protocols: ?string | ?Array, options: ?{headers?: {origin?: string}}) { super(); if (typeof protocols === 'string') { protocols = [protocols]; } + const {headers = {}, ...unrecognized} = options || {}; + + // Preserve deprecated backwards compatibility for the 'origin' option + if (unrecognized && typeof unrecognized.origin === 'string') { + console.warn('Specifying `origin` as a WebSocket connection option is deprecated. Include it under `headers` instead.'); + headers.origin = unrecognized.origin; + delete unrecognized.origin; + } + + // Warn about and discard anything else + if (Object.keys(unrecognized).length > 0) { + console.warn('Unrecognized WebSocket connection option(s) `' + Object.keys(unrecognized).join('`, `') + '`. ' + + 'Did you mean to put these under `headers`?'); + } + if (!Array.isArray(protocols)) { protocols = null; } @@ -111,7 +126,7 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) { this._eventEmitter = new NativeEventEmitter(WebSocketModule); this._socketId = nextWebSocketId++; this._registerEvents(); - WebSocketModule.connect(url, protocols, options, this._socketId); + WebSocketModule.connect(url, protocols, { headers }, this._socketId); } get binaryType(): ?BinaryType { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java index 17df44611..2bfae94e7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java @@ -83,7 +83,7 @@ public final class WebSocketModule extends ReactContextBaseJavaModule { public void connect( final String url, @Nullable final ReadableArray protocols, - @Nullable final ReadableMap headers, + @Nullable final ReadableMap options, final int id) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) @@ -98,7 +98,9 @@ public final class WebSocketModule extends ReactContextBaseJavaModule { builder.addHeader("Cookie", cookie); } - if (headers != null) { + if (options != null && options.hasKey("headers") && options.getType("headers").equals(ReadableType.Map)) { + + ReadableMap headers = options.getMap("headers"); ReadableMapKeySetIterator iterator = headers.keySetIterator(); if (!headers.hasKey("origin")) {