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

socket.io-redis@0.2.0 compatibility fix: roomname format, msgpack format #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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);
}
}
?>