/**
 * 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.
 *
 * @providesModule HMRClient
 */
'use strict';

const invariant = require('invariant');

/**
 * HMR Client that receives from the server HMR updates and propagates them
 * runtime to reflects those changes.
 */
const HMRClient = {
  enable(platform, bundleEntry) {
    invariant(platform, 'Missing required parameter `platform`');
    invariant(bundleEntry, 'Missing required paramenter `bundleEntry`');

    // need to require WebSocket inside of `enable` function because the
    // this module is defined as a `polyfillGlobal`.
    // See `InitializeJavascriptAppEngine.js`
    const WebSocket = require('WebSocket');

    // TODO(martinb): parametrize the url and receive entryFile to minimize
    // the number of updates we want to receive from the server.
    const activeWS = new WebSocket(
      'ws://localhost:8081/hot?platform=' + platform + '&bundleEntry=' +
      bundleEntry.replace('.bundle', '.js')
    );
    activeWS.onerror = (e) => {
      console.error('[Hot Module Replacement] Unexpected error', e);
    };
    activeWS.onmessage = (m) => {
      eval(m.data); // eslint-disable-line no-eval
    };
  },
};

module.exports = HMRClient;

@@ -702,24 +702,137 @@
 eEntry%60');%0A%0A
+    // TODO(martinb) receive host and port as parameters%0A    const host = 'localhost';%0A    const port = '8081';%0A%0A
     // need 
@@ -1040,149 +1040,8 @@
 );%0A%0A
-    // TODO(martinb): parametrize the url and receive entryFile to minimize%0A    // the number of updates we want to receive from the server.%0A
     
@@ -1082,28 +1082,29 @@
     
-'
+%60
 ws://
-localhost:8081
+$%7Bhost%7D:$%7Bport%7D
 /hot
@@ -1117,20 +1117,18 @@
 orm=
-' + 
+$%7B
 platform
  + '
@@ -1127,13 +1127,21 @@
 form
- + '&
+%7D&%60 +%0A      %60
 bund
@@ -1152,18 +1152,10 @@
 try=
-' +%0A      
+$%7B
 bund
@@ -1187,16 +1187,18 @@
 , '.js')
+%7D%60
 %0A    );%0A
@@ -1239,68 +1239,328 @@
     
-console.e
+throw new E
 rror(
-'%5B
+%0A%60
 Hot 
-Module Replacement%5D Unexpected error', e
+loading isn't working because it cannot connect to the development server.%0A%0AEnsure the following:%0A- Node server is running and available on the same network%0A- run 'npm start' from react-native root%0A- Node server URL is correctly set in AppDelegate%0A%0AURL: $%7Bhost%7D:$%7Bport%7D%0A%0AError: $%7Be.message%7D%60%0A      
 );%0A 

Improve error message when devserver is not running