Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"require": {
"php": ">=5.3",
"ptrofimov/tinyredisclient": "1.1.1"
"ptrofimov/tinyredisclient": "1.1.1",
"rybakit/msgpack": "0.9.1"
},
"autoload": {
"psr-4": {
Expand Down
55 changes: 35 additions & 20 deletions src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
define('EVENT', 2);
define('BINARY_EVENT', 5);

if (!function_exists('msgpack_pack')) {
require(__DIR__ . '/msgpack_pack.php');
}

class Emitter {
const UID = "4N8LaD";
const DEFAULT_KEY = 'socket.io';
const DEFAULT_NSP = '/';

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

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

$this->_rooms = array();
$this->_flags = array();
$this->setDefaultFlags();
$this->_exceptRooms = array();
$this->_packer = new \MessagePack\Packer();
}

/*
Expand All @@ -60,6 +62,10 @@ private function readFlag($flag) {
return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false;
}

private function setDefaultFlags() {
$this->_flags = array('nsp' => self::DEFAULT_NSP);
}

/*
* Broadcasting
*/
Expand All @@ -72,6 +78,14 @@ public function in($room) {
return $this;
}

public function except($room) {
if (!in_array($room, $this->_exceptRooms)) {
$this->_exceptRooms[] = $room;
}

return $this;
}

// Alias for in
public function to($room) {
return $this->in($room);
Expand All @@ -83,6 +97,7 @@ public function to($room) {

public function of($nsp) {
$this->_flags['nsp'] = $nsp;

return $this;
}

Expand All @@ -109,33 +124,33 @@ public function emit() {
$packet['data'] = $args;

// set namespace
if (isset($this->_flags['nsp'])) {
$packet['nsp'] = $this->_flags['nsp'];
unset($this->_flags['nsp']);
} else {
$packet['nsp'] = '/';
}
$nsp = $this->_flags['nsp'];
$packet['nsp'] = $nsp;
unset($this->_flags['nsp']);

// publish
$packed = msgpack_pack(array($packet, array(
'rooms' => $this->_rooms,
'flags' => $this->_flags
)));
$opts = array(
'rooms' => $this->_rooms,
'flags' => $this->_flags,
'except' => $this->_exceptRooms,
);

$packed = $this->_packer->pack(array(self::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);
$prefix = $this->key.'#'.$nsp.'#';
$this->redis->publish($prefix, $packed);

// reset state
$this->_rooms = array();
$this->_flags = array();
$this->_exceptRooms = array();
$this->setDefaultFlags();

return $this;
}
}


113 changes: 0 additions & 113 deletions src/msgpack_pack.php

This file was deleted.

2 changes: 2 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testPublishContainsExpectedAttributes() {
$this->assertTrue(strpos($contents, 'yo') !== FALSE);
$this->assertTrue(strpos($contents, 'rooms') !== FALSE);
$this->assertTrue(strpos($contents, 'flags') !== FALSE);
$this->assertTrue(strpos($contents, 'except') !== FALSE);
// Should not broadcast by default
$this->assertFalse(strpos($contents, 'broadcast') !== FALSE);
// Should have the default namespace
Expand All @@ -92,6 +93,7 @@ public function testPublishContainsBroadcastWhenBroadcasting() {
$this->assertTrue(strpos($contents, 'rooms') !== FALSE);
$this->assertTrue(strpos($contents, 'flags') !== FALSE);
$this->assertTrue(strpos($contents, 'broadcast') !== FALSE);
$this->assertTrue(strpos($contents, 'except') !== FALSE);
}

public function testPublishContainsExpectedDataWhenEmittingBinary() {
Expand Down