title: Arduino WebSocket Client connection issue with node.js ws websocket server
body: <p>I'm trying to make a small WiFi control board with the use of an ESP8266 ESP-201 board.</p>
  
  <p>I used the example provided for WebSocket for Arduino, with a little bit of modification to be able to handle JSON messages. This is the code I got to:</p>
  
  <pre><code>/*
   * WebSocketClient.ino
   *
   *  Created on: 24.05.2015
   *
   */
  
  #include &lt;Arduino.h&gt;
  #include &lt;ESP8266WiFi.h&gt;
  #include &lt;ESP8266WiFiMulti.h&gt;
  #include &lt;WebSocketsClient.h&gt;
  #include &lt;ArduinoJson.h&gt;
  #include &lt;Hash.h&gt;
  
  ESP8266WiFiMulti WiFiMulti;
  WebSocketsClient webSocket;
  
  #define USE_SERIAL Serial
  ArduinoJson::StaticJsonBuffer&lt;200&gt; jsonBuffer;
  
  void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
    switch(type) {
      case WStype_DISCONNECTED:
        USE_SERIAL.printf("[WSc] Disconnected!\n");
        break;
      case WStype_CONNECTED: {
        USE_SERIAL.printf("[WSc] Connected to url: %s\n",  payload);
        // send message to server when Connected
      }
      break;
      case WStype_TEXT: {
        USE_SERIAL.printf("[WSc] get text: %s\n", payload);
        String text = String((char *) &amp;payload[0]);
        USE_SERIAL.println(text);
        JsonObject&amp; root = jsonBuffer.parseObject(text);
        USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["r"]);
        USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["g"]);
        USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["b"]);
      }
      // send message to server
      break;
      case WStype_BIN:
        USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
        hexdump(payload, lenght);
        // send data to server
        break;
      }
  }
  
  void setup() {
    USE_SERIAL.begin(115200);
    USE_SERIAL.setDebugOutput(true);
    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();
    for(uint8_t t = 4; t &gt; 0; t--) {
      USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
      USE_SERIAL.flush();
      delay(1000);
    }
    WiFiMulti.addAP("GamersHeavenLow", "nCore4Life");
    while(WiFiMulti.run() != WL_CONNECTED) {
      delay(100);
    }
    webSocket.begin("192.168.0.104", 3000);
    webSocket.onEvent(webSocketEvent);
  }
  
  void loop() {
    webSocket.loop();
  }
  </code></pre>
  
  <p>And this is the server side I used to: </p>
  
  <pre><code>var express = require('express');
  var app = express();
  var url = require('url');
  var http = require('http').Server(app);
  var WebSocketServer = require('ws').Server;
  var wss = new WebSocketServer({ server: http });
  app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
  });
  app.use(express.static('public'));
  wss.on('error', function(error) {
    console.log(error);
  });
  wss.on('connection', function connection(ws) {
    var location = url.parse(ws.upgradeReq.url, true);
    // you might use location.query.access_token to authenticate or share sessions
    // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
    console.log('connected');
    ws.on('message', function incoming(message) {
      console.log(message);
      wss.clients.forEach(function each(client) {
        client.send(JSON.stringify(message));
      });
    });
    //ws.send('something');
  });
  
  http.listen(3000, function(){
    console.log('listening on *:3000');
  });
  </code></pre>
  
  <p>Before I started to work with the JSON encoding it was working for a while, but now it's not. I get the following error:</p>
  
  <pre><code>[SETUP] BOOT WAIT 4...
  scandone
  state: 0 -&gt; 2 (b0)
  state: 2 -&gt; 3 (0)
  state: 3 -&gt; 5 (10)
  add 0
  aid 2
  cnt 
  
  connected with GamersHeavenLow, channel 6
  dhcp client start...
  ip:192.168.0.101,mask:255.255.255.0,gw:192.168.0.1
  [SETUP] BOOT WAIT 3...
  [SETUP] BOOT WAIT 2...
  [SETUP] BOOT WAIT 1...
  [WS-Client] connect ws...
  [WS-Client] connection to 192.168.0.104:3000 Faild
  [WS-Client] client disconnected.
  [WSc] Disconnected!
  [WS-Client] connect ws...
  [WS-Client] connection to 192.168.0.104:3000 Faild
  [WS-Client] client disconnected.
  [WSc] Disconnected!
  </code></pre>
  
  <p>I believed it must have been the JSON decoding, so I reverted back to the default example, but I still get the same message, connection failed.</p>
  
  <p>So I tried to use the echo websocket server, with which the Arduino code started working.</p>
  
  <p>So I figured out it must be a server side issue, so I tested my node.js websocket server with a pure WebSocket client, but that is also working without a problem.</p>
  
  <p>So basically I have two separate sets of code, which are running in isolation from one another without a problem, but they don't want to play along together. </p>
  
  <p>Any idea what might be causing this? </p>
  
  <p>WebSocket server used: <a href="https://github.com/websockets/ws" rel="nofollow noreferrer">https://github.com/websockets/ws</a></p>
  
  <p>WebSocket client used on ardruino: <a href="https://github.com/Links2004/arduinoWebSockets" rel="nofollow noreferrer">https://github.com/Links2004/arduinoWebSockets</a></p>