{"action":"created","issue":{"url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/12","labels_url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/12/comments","events_url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/12/events","html_url":"https://github.com/leg0/Arduino-Websocket/issues/12","id":53309950,"number":12,"title":"trouble to use this library with Adafruit_cc3000 library","user":{"login":"taher143","id":3510638,"avatar_url":"https://avatars.githubusercontent.com/u/3510638?v=3","gravatar_id":"","url":"https://api.github.com/users/taher143","html_url":"https://github.com/taher143","followers_url":"https://api.github.com/users/taher143/followers","following_url":"https://api.github.com/users/taher143/following{/other_user}","gists_url":"https://api.github.com/users/taher143/gists{/gist_id}","starred_url":"https://api.github.com/users/taher143/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taher143/subscriptions","organizations_url":"https://api.github.com/users/taher143/orgs","repos_url":"https://api.github.com/users/taher143/repos","events_url":"https://api.github.com/users/taher143/events{/privacy}","received_events_url":"https://api.github.com/users/taher143/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/leg0/Arduino-Websocket/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-03T17:33:48Z","updated_at":"2015-01-08T01:55:17Z","closed_at":null,"body":"I am having trouble with testing the web-socket client with cc3000 arduino. I have taken reference files from brandenhall/Arduino-Websocket repository. I am not able to connect to the server through client. getting bad request 404 error or time out error each time. \r\nPlease explain me the way to test it locally.\r\nhere is my code which I have used for arduino client.\r\n\r\n#include <Base64.h>\r\n#include <global.h>\r\n#include <MD5.h>\r\n#include <sha1.h>\r\n#include <WebSocketClient.h>\r\n\r\n/*************************************************** \r\n  This is an example for the Adafruit CC3000 Wifi Breakout & Shield\r\n\r\n  Designed specifically to work with the Adafruit WiFi products:\r\n  ----> https://www.adafruit.com/products/1469\r\n\r\n  Adafruit invests time and resources providing this open source code, \r\n  please support Adafruit and open-source hardware by purchasing \r\n  products from Adafruit!\r\n\r\n  Written by Limor Fried & Kevin Townsend for Adafruit Industries.  \r\n  BSD license, all text above must be included in any redistribution\r\n ****************************************************/\r\n \r\n /*\r\nThis example does a test of the TCP client capability:\r\n  * Initialization\r\n  * Optional: SSID scan\r\n  * AP connection\r\n  * DHCP printout\r\n  * DNS lookup\r\n  * Optional: Ping\r\n  * Connect to website and print out webpage contents\r\n  * Disconnect\r\nSmartConfig is still beta and kind of works but is not fully vetted!\r\nIt might not work on all networks!\r\n*/\r\n#include <Adafruit_CC3000.h>\r\n#include <ccspi.h>\r\n#include <SPI.h>\r\n#include <string.h>\r\n#include \"utility/debug.h\"\r\n#define MAX_FRAME_LENGTH 64\r\n\r\n// Define how many callback functions you have. Default is 1.\r\n#define CALLBACK_FUNCTIONS 1\r\n\r\n#include <WebSocketClient.h>\r\n\r\n\r\nWebSocketClient webSocketClient;\r\n// These are the interrupt and control pins\r\n#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!\r\n// These can be any two pins\r\n#define ADAFRUIT_CC3000_VBAT  5\r\n#define ADAFRUIT_CC3000_CS    10\r\n// Use hardware SPI for the remaining pins\r\n// On an UNO, SCK = 13, MISO = 12, and MOSI = 11\r\nAdafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,\r\n                                         SPI_CLOCK_DIVIDER); // you can change this clock speed\r\n\r\n#define WLAN_SSID       \"UNSTOPPABLE\"           // cannot be longer than 32 characters!\r\n#define WLAN_PASS       \"8878333344\"\r\n// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2\r\n#define WLAN_SECURITY   WLAN_SEC_WPA2\r\n\r\n#define IDLE_TIMEOUT_MS  3000      // Amount of time to wait (in milliseconds) with no data \r\n                                   // received before closing the connection.  If you know the server\r\n                                   // you're accessing is quick to respond, you can reduce this value.\r\n\r\n// What page to grab!\r\n#define WEBSITE      \"www.pravelsolutions.com\"\r\n#define WEBPAGE      \"/aspero/activation.php?name=\\\"Aspero_1\\\"&serial=0000000001\"\r\n\r\n\r\n/**************************************************************************/\r\n/*!\r\n    @brief  Sets up the HW and the CC3000 module (called automatically\r\n            on startup)\r\n*/\r\n/**************************************************************************/\r\n\r\nuint32_t ip;\r\n\r\nvoid setup(void)\r\n{\r\n  String data;\r\n  Serial.begin(115200);\r\n  Serial.println(F(\"Hello, CC3000!\\n\")); \r\n\r\n  Serial.print(\"Free RAM: \"); Serial.println(getFreeRam(), DEC);\r\n  \r\n  /* Initialise the module */\r\n  Serial.println(F(\"\\nInitializing...\"));\r\n  if (!cc3000.begin())\r\n  {\r\n    Serial.println(F(\"Couldn't begin()! Check your wiring?\"));\r\n    while(1);\r\n  }\r\n  \r\n  // Optional SSID scan\r\n  // listSSIDResults();\r\n  \r\n  Serial.print(F(\"\\nAttempting to connect to \")); Serial.println(WLAN_SSID);\r\n  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {\r\n    Serial.println(F(\"Failed!\"));\r\n    while(1);\r\n  }\r\n   \r\n  Serial.println(F(\"Connected!\"));\r\n  \r\n  /* Wait for DHCP to complete */\r\n  Serial.println(F(\"Request DHCP\"));\r\n  while (!cc3000.checkDHCP())\r\n  {\r\n    delay(100); // ToDo: Insert a DHCP timeout!\r\n  }  \r\n\r\n  /* Display the IP address DNS, Gateway, etc. */  \r\n  while (! displayConnectionDetails()) {\r\n    delay(1000);\r\n  }\r\n\r\n  ip = 0;\r\n  // Try looking up the website's IP address\r\n  Serial.print(WEBSITE); Serial.print(F(\" -> \"));\r\n  while (ip == 0) {\r\n    if (! cc3000.getHostByName(WEBSITE, &ip)) {\r\n      Serial.println(F(\"Couldn't resolve!\"));\r\n    }\r\n    delay(500);\r\n  }\r\n\r\n  cc3000.printIPdotsRev(ip);\r\n  \r\n  // Optional: Do a ping test on the website\r\n  /*\r\n  Serial.print(F(\"\\n\\rPinging \")); cc3000.printIPdotsRev(ip); Serial.print(\"...\");  \r\n  replies = cc3000.ping(ip, 5);\r\n  Serial.print(replies); Serial.println(F(\" replies\"));\r\n  */  \r\n\r\n  /* Try connecting to the website.\r\n     Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.\r\n  */\r\n  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);\r\n  if (www.connected()) {\r\n    Serial.println(F(\"Connected.\"));\r\n  /*  www.fastrprint(F(\"GET \"));\r\n    www.fastrprint(WEBPAGE);\r\n    www.fastrprint(F(\" HTTP/1.1\\r\\n\"));\r\n    www.fastrprint(F(\"Host: \")); www.fastrprint(WEBSITE); www.fastrprint(F(\"\\r\\n\"));\r\n    www.fastrprint(F(\"\\r\\n\"));\r\n    www.println();*/\r\n  } else {\r\n    Serial.println(F(\"Connection failed\"));    \r\n    return;\r\n  }\r\n\r\n  Serial.println(F(\"-------------------------------------\"));\r\n  \r\n  /* Read data until either the connection is closed, or the idle timeout is reached. */ \r\n/*  unsigned long lastRead = millis();\r\n  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {\r\n    while (www.available()) {\r\n      char c = www.read();\r\n      Serial.print(c);\r\n      lastRead = millis();\r\n    }\r\n  }\r\n  www.close();\r\n  Serial.println(F(\"-------------------------------------\"));*/\r\n  \r\n  /* You need to make sure to clean up after yourself or the CC3000 can freak out */\r\n  /* the next time your try to connect ... */\r\n//  Serial.println(F(\"\\n\\nDisconnecting\"));\r\n//  cc3000.disconnect();\r\n// Handshake with the server\r\n  webSocketClient.path = \"/\";\r\n  webSocketClient.host = \"www.pravelsolutions.com\";\r\n  \r\n  if (webSocketClient.handshake(www)) {\r\n    Serial.println(\"Handshake successful\");\r\n  } else {\r\n    Serial.println(\"Handshake failed.\");\r\n    while(1) {\r\n      // Hang on failure\r\n    }  \r\n  }\r\n  \r\n  if (www.connected()) {\r\n    \r\n /*   data = webSocketClient.getData();\r\n\r\n    if (data.length() > 0) {\r\n      Serial.print(\"Received data: \");\r\n      Serial.println(data);\r\n    }*/\r\n    \r\n    // capture the value of analog 1, send it along\r\n    pinMode(1, INPUT);\r\n    data = String(analogRead(1));\r\n    \r\n    webSocketClient.sendData(data);\r\n    \r\n  } else {\r\n    \r\n    Serial.println(\"Client disconnected.\");\r\n    while (1) {\r\n      // Hang on disconnect.\r\n    }\r\n  }\r\n  \r\n  // wait to fully let the client disconnect\r\n  delay(3000);\r\n  \r\n}\r\n\r\nvoid loop(void)\r\n{\r\n// delay(1000);\r\n \r\n  \r\n}\r\n\r\n/**************************************************************************/\r\n/*!\r\n    @brief  Begins an SSID scan and prints out all the visible networks\r\n*/\r\n/**************************************************************************/\r\n\r\nvoid listSSIDResults(void)\r\n{\r\n  uint32_t index;\r\n  uint8_t valid, rssi, sec;\r\n  char ssidname[33]; \r\n\r\n  if (!cc3000.startSSIDscan(&index)) {\r\n    Serial.println(F(\"SSID scan failed!\"));\r\n    return;\r\n  }\r\n\r\n  Serial.print(F(\"Networks found: \")); Serial.println(index);\r\n  Serial.println(F(\"================================================\"));\r\n\r\n  while (index) {\r\n    index--;\r\n\r\n    valid = cc3000.getNextSSID(&rssi, &sec, ssidname);\r\n    \r\n    Serial.print(F(\"SSID Name    : \")); Serial.print(ssidname);\r\n    Serial.println();\r\n    Serial.print(F(\"RSSI         : \"));\r\n    Serial.println(rssi);\r\n    Serial.print(F(\"Security Mode: \"));\r\n    Serial.println(sec);\r\n    Serial.println();\r\n  }\r\n  Serial.println(F(\"================================================\"));\r\n\r\n  cc3000.stopSSIDscan();\r\n}\r\n\r\n/**************************************************************************/\r\n/*!\r\n    @brief  Tries to read the IP address and other connection details\r\n*/\r\n/**************************************************************************/\r\nbool displayConnectionDetails(void)\r\n{\r\n  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;\r\n  \r\n  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))\r\n  {\r\n    Serial.println(F(\"Unable to retrieve the IP Address!\\r\\n\"));\r\n    return false;\r\n  }\r\n  else\r\n  {\r\n    Serial.print(F(\"\\nIP Addr: \")); cc3000.printIPdotsRev(ipAddress);\r\n    Serial.print(F(\"\\nNetmask: \")); cc3000.printIPdotsRev(netmask);\r\n    Serial.print(F(\"\\nGateway: \")); cc3000.printIPdotsRev(gateway);\r\n    Serial.print(F(\"\\nDHCPsrv: \")); cc3000.printIPdotsRev(dhcpserv);\r\n    Serial.print(F(\"\\nDNSserv: \")); cc3000.printIPdotsRev(dnsserv);\r\n    Serial.println();\r\n    return true;\r\n  }\r\n}\r\n\r\nHere is a server code which i have used:\r\n#!/php -q\r\n<?php  /*  >php -q server.php  */\r\n\r\nerror_reporting(E_ALL);\r\nset_time_limit(0);\r\nob_implicit_flush();\r\n\r\n$master  = WebSocket(\"localhost\",12345);\r\n$sockets = array($master);\r\n$users   = array();\r\n$debug   = false;\r\n\r\nwhile(true){\r\n  $changed = $sockets;\r\n  socket_select($changed,$write=NULL,$except=NULL,NULL);\r\n  foreach($changed as $socket){\r\n    if($socket==$master){\r\n      $client=socket_accept($master);\r\n      if($client<0){ console(\"socket_accept() failed\"); continue; }\r\n      else{ connect($client); }\r\n    }\r\n    else{\r\n      $bytes = @socket_recv($socket,$buffer,2048,0);\r\n      if($bytes==0){ disconnect($socket); }\r\n      else{\r\n        $user = getuserbysocket($socket);\r\n        if(!$user->handshake){ dohandshake($user,$buffer); }\r\n        else{ process($user,$buffer); }\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\n//---------------------------------------------------------------\r\nfunction process($user,$msg){\r\n  $action = unwrap($msg);\r\n  say(\"< \".$action);\r\n  switch($action){\r\n    case \"hello\" : send($user->socket,\"hello human\");                       break;\r\n    case \"hi\"    : send($user->socket,\"zup human\");                         break;\r\n    case \"name\"  : send($user->socket,\"my name is Multivac, silly I know\"); break;\r\n    case \"age\"   : send($user->socket,\"I am older than time itself\");       break;\r\n    case \"date\"  : send($user->socket,\"today is \".date(\"Y.m.d\"));           break;\r\n    case \"time\"  : send($user->socket,\"server time is \".date(\"H:i:s\"));     break;\r\n    case \"thanks\": send($user->socket,\"you're welcome\");                    break;\r\n    case \"bye\"   : send($user->socket,\"bye\");                               break;\r\n    default      : send($user->socket,$action.\" not understood\");           break;\r\n  }\r\n}\r\n\r\nfunction send($client,$msg){\r\n  say(\"> \".$msg);\r\n  $msg = wrap($msg);\r\n  socket_write($client,$msg,strlen($msg));\r\n}\r\n\r\nfunction WebSocket($address,$port){\r\n  $master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die(\"socket_create() failed\");\r\n  socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1)  or die(\"socket_option() failed\");\r\n  socket_bind($master, $address, $port)                    or die(\"socket_bind() failed\");\r\n  socket_listen($master,20)                                or die(\"socket_listen() failed\");\r\n  echo \"Server Started : \".date('Y-m-d H:i:s').\"\\n\";\r\n  echo \"Master socket  : \".$master.\"\\n\";\r\n  echo \"Listening on   : \".$address.\" port \".$port.\"\\n\\n\";\r\n  return $master;\r\n}\r\n\r\nfunction connect($socket){\r\n  global $sockets,$users;\r\n  $user = new User();\r\n  $user->id = uniqid();\r\n  $user->socket = $socket;\r\n  array_push($users,$user);\r\n  array_push($sockets,$socket);\r\n  console($socket.\" CONNECTED!\");\r\n}\r\n\r\nfunction disconnect($socket){\r\n  global $sockets,$users;\r\n  $found=null;\r\n  $n=count($users);\r\n  for($i=0;$i<$n;$i++){\r\n    if($users[$i]->socket==$socket){ $found=$i; break; }\r\n  }\r\n  if(!is_null($found)){ array_splice($users,$found,1); }\r\n  $index = array_search($socket,$sockets);\r\n  socket_close($socket);\r\n  console($socket.\" DISCONNECTED!\");\r\n  if($index>=0){ array_splice($sockets,$index,1); }\r\n}\r\n\r\nfunction dohandshake($user,$buffer){\r\n  console(\"\\nRequesting handshake...\");\r\n  console($buffer);\r\n  list($resource,$host,$origin,$strkey1,$strkey2,$data) = getheaders($buffer);\r\n  console(\"Handshaking...\");\r\n\r\n  $pattern = '/[^\\d]*/';\r\n  $replacement = '';\r\n  $numkey1 = preg_replace($pattern, $replacement, $strkey1);\r\n  $numkey2 = preg_replace($pattern, $replacement, $strkey2);\r\n\r\n  $pattern = '/[^ ]*/';\r\n  $replacement = '';\r\n  $spaces1 = strlen(preg_replace($pattern, $replacement, $strkey1));\r\n  $spaces2 = strlen(preg_replace($pattern, $replacement, $strkey2));\r\n\r\n  if ($spaces1 == 0 || $spaces2 == 0 || $numkey1 % $spaces1 != 0 || $numkey2 % $spaces2 != 0) {\r\n﻿  socket_close($user->socket);\r\n﻿  console('failed');\r\n﻿  return false;\r\n  }\r\n\r\n  $ctx = hash_init('md5');\r\n  hash_update($ctx, pack(\"N\", $numkey1/$spaces1));\r\n  hash_update($ctx, pack(\"N\", $numkey2/$spaces2));\r\n  hash_update($ctx, $data);\r\n  $hash_data = hash_final($ctx,true);\r\n\r\n  $upgrade  = \"HTTP/1.1 101 WebSocket Protocol Handshake\\r\\n\" .\r\n              \"Upgrade: WebSocket\\r\\n\" .\r\n              \"Connection: Upgrade\\r\\n\" .\r\n              \"Sec-WebSocket-Origin: \" . $origin . \"\\r\\n\" .\r\n              \"Sec-WebSocket-Location: ws://\" . $host . $resource . \"\\r\\n\" .\r\n              \"\\r\\n\" .\r\n              $hash_data;\r\n\r\n  socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));\r\n  $user->handshake=true;\r\n  console($upgrade);\r\n  console(\"Done handshaking...\");\r\n  return true;\r\n}\r\n\r\nfunction getheaders($req){\r\n  $r=$h=$o=null;\r\n  if(preg_match(\"/GET (.*) HTTP/\"   ,$req,$match)){ $r=$match[1]; }\r\n  if(preg_match(\"/Host: (.*)\\r\\n/\"  ,$req,$match)){ $h=$match[1]; }\r\n  if(preg_match(\"/Origin: (.*)\\r\\n/\",$req,$match)){ $o=$match[1]; }\r\n  if(preg_match(\"/Sec-WebSocket-Key2: (.*)\\r\\n/\",$req,$match)){ $key2=$match[1]; }\r\n  if(preg_match(\"/Sec-WebSocket-Key1: (.*)\\r\\n/\",$req,$match)){ $key1=$match[1]; }\r\n  if(preg_match(\"/\\r\\n(.*?)\\$/\",$req,$match)){ $data=$match[1]; }\r\n  return array($r,$h,$o,$key1,$key2,$data);\r\n}\r\n\r\nfunction getuserbysocket($socket){\r\n  global $users;\r\n  $found=null;\r\n  foreach($users as $user){\r\n    if($user->socket==$socket){ $found=$user; break; }\r\n  }\r\n  return $found;\r\n}\r\n\r\nfunction     say($msg=\"\"){ echo $msg.\"\\n\"; }\r\nfunction    wrap($msg=\"\"){ return chr(0).$msg.chr(255); }\r\nfunction  unwrap($msg=\"\"){ return substr($msg,1,strlen($msg)-2); }\r\nfunction console($msg=\"\"){ global $debug; if($debug){ echo $msg.\"\\n\"; } }\r\n\r\nclass User{\r\n  var $id;\r\n  var $socket;\r\n  var $handshake;\r\n}\r\n\r\n?>\r\n\r\nAlso I am not able to find getdata() function from WebsocketClient.cpp fro which I was getting error."},"comment":{"url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/comments/69124889","html_url":"https://github.com/leg0/Arduino-Websocket/issues/12#issuecomment-69124889","issue_url":"https://api.github.com/repos/leg0/Arduino-Websocket/issues/12","id":69124889,"user":{"login":"leg0","id":207016,"avatar_url":"https://avatars.githubusercontent.com/u/207016?v=3","gravatar_id":"","url":"https://api.github.com/users/leg0","html_url":"https://github.com/leg0","followers_url":"https://api.github.com/users/leg0/followers","following_url":"https://api.github.com/users/leg0/following{/other_user}","gists_url":"https://api.github.com/users/leg0/gists{/gist_id}","starred_url":"https://api.github.com/users/leg0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leg0/subscriptions","organizations_url":"https://api.github.com/users/leg0/orgs","repos_url":"https://api.github.com/users/leg0/repos","events_url":"https://api.github.com/users/leg0/events{/privacy}","received_events_url":"https://api.github.com/users/leg0/received_events","type":"User","site_admin":false},"created_at":"2015-01-08T01:55:17Z","updated_at":"2015-01-08T01:55:17Z","body":"Here are some things you can try:\r\n* use a debugger\r\n* put some logging statements in places you suspect the problem to be\r\n* use a network monitoring tool to see what data is being sent/received."}}