Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesnt work on Ubuntu server 16.04 with php7 #23

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

### *Maintained Fork*

This is a fork of [rase-/socket.io-php-emitter](https://github.com/rase-/socket.io-php-emitter).
Since the original repo is not being maintained anymore and is having compatibility problems with
newer versions of Socket.io, I recommend using this repo, and also submitting issues here.

[@ashiina](https://github.com/ashiina)

socket.io-php-emitter
=====================

Expand All @@ -6,7 +15,7 @@ A PHP implementation of socket.io-emitter.
This project requires a Redis client for PHP. If you dont have the [PECL Redis](https://github.com/nicolasff/phpredis) installed, the emitter will default to using [TinyRedisClient](https://github.com/ptrofimov/tinyredisclient). You can, however, pass in any Redis client that supports a `publish` method.

## Installation and development
To install and use in your PHP project, install it as a [composer package](https://packagist.org/packages/rase/socket.io-emitter). Install dependencies with `composer install`.
To install and use in your PHP project, install it as a [composer package](https://packagist.org/packages/ashiina/socket.io-emitter). Install dependencies with `composer install`.

To run tests, invoke `make test`. The current test suite will just be checking redis monitor that everything is published correctly. Some work will be put into making a better integration test suite in the near future.

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "rase/socket.io-emitter",
"description": "A PHP implementation of socket.io-emitter",
"homepage": "https://github.com/rase-/socket.io-php-emitter",
"name": "ashiina/socket.io-emitter",
"description": "*Maintained* A PHP implementation of socket.io-emitter",
"homepage": "https://github.com/ashiina/socket.io-php-emitter",
"support": {
"issues": "https://github.com/rase-/socket.io-php-emitter/issues",
"source": "https://github.com/rase-/socket.io-php-emitter"
"issues": "https://github.com/ashiina/socket.io-php-emitter/issues",
"source": "https://github.com/ashiina/socket.io-php-emitter"
},
"license": "MIT",
"keywords": [
Expand Down
21 changes: 16 additions & 5 deletions src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
}

class Emitter {
private $uid = 'emitter';

public function __construct($redis = FALSE, $opts = array()) {
if (is_array($redis)) {
$opts = $redis;
Expand Down Expand Up @@ -41,7 +43,7 @@ public function __construct($redis = FALSE, $opts = array()) {
}

$this->redis = $redis;
$this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io') . '#emitter';
$this->prefix = isset($opts['key']) ? $opts['key'] : 'socket.io';

$this->_rooms = array();
$this->_flags = array();
Expand Down Expand Up @@ -116,19 +118,28 @@ public function emit() {
$packet['nsp'] = '/';
}

// publish
$packed = msgpack_pack(array($packet, array(
$opts = array(
'rooms' => $this->_rooms,
'flags' => $this->_flags
)));
);
$chn = $this->prefix . '#' . $packet['nsp'] . '#';
$packed = msgpack_pack(array($this->uid,$packet,$opts));

// hack buffer extensions for msgpack with binary
if ($packet['type'] == BINARY_EVENT) {
$packed = str_replace(pack('c', 0xda), pack('c', 0xd8), $packed);
$packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed);
}

$this->redis->publish($this->key, $packed);
// publish
if (is_array($this->_rooms) && count($this->_rooms) > 0) {
foreach ($this->_rooms as $room) {
$chnRoom = $chn . $room . '#';
$this->redis->publish($chnRoom, $packed);
}
} else {
$this->redis->publish($chn, $packed);
}

// reset state
$this->_rooms = array();
Expand Down
45 changes: 45 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,50 @@ public function testPublishContainsNamespaceWhenEmittingWithNamespaceSet() {

$this->assertTrue(strpos($contents, '/nsp') !== FALSE);
}

public function testPublishKeyNameWithNamespaceSet() {
$p = new Process('redis-cli monitor > redis.log');

sleep(1);
// Running this should produce something that's visible in `redis-cli monitor`
$emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379'));
$emitter->of('/nsp')->emit('yolo', 'data');

$p->stop();
$contents= file_get_contents('redis.log');
unlink('redis.log');

$this->assertTrue(strpos($contents, 'socket.io#/nsp#') !== FALSE);
}

public function testPublishKeyNameWithRoomSet() {
$p = new Process('redis-cli monitor > redis.log');

sleep(1);
// Running this should produce something that's visible in `redis-cli monitor`
$emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379'));
$emitter->to('rm')->emit('yolo', 'data');

$p->stop();
$contents= file_get_contents('redis.log');
unlink('redis.log');

$this->assertTrue(strpos($contents, 'socket.io#/#rm#') !== FALSE);
}

public function testPublishKeyNameWithNamespaceAndRoomSet() {
$p = new Process('redis-cli monitor > redis.log');

sleep(1);
// Running this should produce something that's visible in `redis-cli monitor`
$emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379'));
$emitter->of('/nsp')->to('rm')->emit('yolo', 'data');

$p->stop();
$contents= file_get_contents('redis.log');
unlink('redis.log');

$this->assertTrue(strpos($contents, 'socket.io#/nsp#rm#') !== FALSE);
}
}
?>