Skip to content

Instantly share code, notes, and snippets.

@JumpLink
Created November 6, 2013 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JumpLink/7341184 to your computer and use it in GitHub Desktop.
Save JumpLink/7341184 to your computer and use it in GitHub Desktop.
socket-io client in php for sails.js. NOT WORKING!
<?php
require_once('HTTP/Request2.php'); // sudo pear install HTTP_Request2
require( __DIR__ . '/vendor/wisembly/elephant.io/lib/ElephantIO/Client.php');
use ElephantIO\Client as ElephantIOClient; // sudo apt-get install php5-curl
function get_cookie($request) {
$request->setMethod( HTTP_Request2::METHOD_GET);
$request->setCookieJar(true);
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$cookie = $response->getCookies()[0];
$request->setCookie($cookie["name"], $cookie["value"]);
$request->setHeader('cookie', $cookie["name"].'='.$cookie["value"]);
return $cookie;
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
return false;
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
return false;
}
function request($request) {
$request->setMethod(HTTP_Request2::METHOD_POST);
try {
$response = $request->send();
if (200 == $response->getStatus()) {
//echo $response->getBody();
return true;
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
return false;
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
return false;
}
$url = "http://localhost:1337";
$request = new HTTP_Request2($url);
$cookie = get_cookie($request);
$socket_url = $url."?cookie=".$cookie["value"];
print ($socket_url);
$elephant = new ElephantIOClient($socket_url, 'socket.io', 1, false, true, true);
$elephant->init();
$elephant->send(
ElephantIOClient::TYPE_EVENT,
null,
null,
json_encode(array('name' => 'action', 'args' => 'foo'))
);
$elephant->close();
echo 'tryin to send `foo` to the event called action';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment