Skip to content

Instantly share code, notes, and snippets.

@joseph-montanez
Created October 24, 2011 20:40
Show Gist options
  • Save joseph-montanez/1310150 to your computer and use it in GitHub Desktop.
Save joseph-montanez/1310150 to your computer and use it in GitHub Desktop.
Connecting to nodejs
<?php
class instant_notice {
public $sid = '';
public $host = 'example.com';
public $port = '1337';
public function connect() {
//----------------------------------------------------------------
//-- Get Handshack
//----------------------------------------------------------------
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => ''
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://' . $this->host . ':' . $this->port . '/socket.io/1/', false, $context);
$sessid = explode(':', $result);
$this->sid = $sessid[0];
//----------------------------------------------------------------
//-- Confirm the connection
//----------------------------------------------------------------
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Content-Type: text/plain;charset=UTF-8'
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://' . $this->host . ':' . $this->port . '/socket.io/1/xhr-polling/' . $this->sid . '?t=' . strtotime('now'), false, $context);
}
public function send(array $data) {
//----------------------------------------------------------------
//-- Send the notice
//----------------------------------------------------------------
$postdata = '5:::{"name":"send-notice","args":[' . json_encode($data) . ']}';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: text/plain;charset=UTF-8',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://' . $this->host . ':' . $this->port . '/socket.io/1/xhr-polling/' . $this->sid . '?t=' . strtotime('now'), false, $context);
}
public function disconnect() {
//----------------------------------------------------------------
//-- Send the disconnect
//----------------------------------------------------------------
$postdata = '0::';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: text/plain;charset=UTF-8',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://' . $this->host . ':' . $this->port . '/socket.io/1/xhr-polling/' . $this->sid . '?t=' . strtotime('now'), false, $context);
}
public static function push(array $data) {
$in = new instant_notice();
$in->connect();
$in->send($data);
$in->disconnect();
}
}
?>
<?php
instant_notice::push(array(
'members_id' => '24',
'notice' => 'Testing from the PHP side! - ' . uniqid()
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment