Skip to content

Commit

Permalink
i36: Updates package to work with socket.io v4
Browse files Browse the repository at this point in the history
- Replaces checked in mspack_pack.php with rybakit/msgpack composer
  package
- Adds UID to packed message
- Removes "#emitter" suffix on key

fixes rase-#36
  • Loading branch information
Rob Gonnella committed Aug 18, 2022
1 parent 6756a34 commit f53b2e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 125 deletions.
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
31 changes: 20 additions & 11 deletions src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
define('EVENT', 2);
define('BINARY_EVENT', 5);

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

class Emitter {
const UID = "4N8LaD";

public function __construct($redis = FALSE, $opts = array()) {
if (is_array($redis)) {
$opts = $redis;
Expand Down Expand Up @@ -41,10 +39,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'] : 'socket.io');

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

/*
Expand Down Expand Up @@ -72,6 +72,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 Down Expand Up @@ -117,10 +125,13 @@ public function emit() {
}

// 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) {
Expand All @@ -137,5 +148,3 @@ public function emit() {
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

0 comments on commit f53b2e3

Please sign in to comment.