-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement message pack serializer
- Loading branch information
Showing
14 changed files
with
263 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Helper; | ||
|
||
use Octamp\Wamp\Serializer\JsonSerializer; | ||
use Octamp\Wamp\Serializer\MessagePackSerializer; | ||
use Octamp\Wamp\Serializer\WampMessageSerializerInterface; | ||
|
||
class SerializerHelper | ||
{ | ||
public static function getSerializer(string $protocol): ?WampMessageSerializerInterface | ||
{ | ||
if ($protocol === 'wamp.2.json') { | ||
return new JsonSerializer(); | ||
} | ||
|
||
if ($protocol === 'wamp.2.msgpack') { | ||
return new MessagePackSerializer(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function supportedProtocols(): array | ||
{ | ||
return [ | ||
'wamp.2.msgpack', | ||
'wamp.2.json', | ||
]; | ||
} | ||
|
||
public static function getFirstSupportedProtocols(array $userProtocols): ?string | ||
{ | ||
$supportedProtocols = static::supportedProtocols(); | ||
foreach ($supportedProtocols as $protocol) { | ||
if (in_array($protocol, $userProtocols)) { | ||
return trim($protocol); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Serializer; | ||
|
||
class DeserializationException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Serializer; | ||
|
||
use OpenSwoole\WebSocket\Server; | ||
use Thruway\Message\Message; | ||
|
||
class JsonSerializer implements WampMessageSerializerInterface | ||
{ | ||
public function serialize(Message $msg): string | ||
{ | ||
return json_encode($msg); | ||
} | ||
|
||
public function deserialize(string $serializedData): Message | ||
{ | ||
if (null === ($data = @json_decode($serializedData))) { | ||
throw new DeserializationException("Error decoding json \"" . $serializedData . "\""); | ||
} | ||
|
||
return Message::createMessageFromArray($data); | ||
} | ||
|
||
public function protocolName(): string | ||
{ | ||
return 'wamp.2.json'; | ||
} | ||
|
||
public function opcode(): int | ||
{ | ||
return Server::WEBSOCKET_OPCODE_TEXT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Serializer; | ||
|
||
use MessagePack\MessagePack; | ||
use MessagePack\Packer; | ||
use OpenSwoole\WebSocket\Server; | ||
use Thruway\Message\Message; | ||
|
||
class MessagePackSerializer implements WampMessageSerializerInterface | ||
{ | ||
|
||
public function serialize(Message $msg): string | ||
{ | ||
$data = json_encode($msg->getMessageParts()); | ||
$data = json_decode($data, true); | ||
|
||
return MessagePack::pack($data); | ||
} | ||
|
||
public function deserialize(string $serializedData): Message | ||
{ | ||
$data = MessagePack::unpack($serializedData); | ||
|
||
return Message::createMessageFromArray($data); | ||
} | ||
|
||
public function protocolName(): string | ||
{ | ||
return 'wamp.2.msgpack'; | ||
} | ||
|
||
public function opcode(): int | ||
{ | ||
return Server::WEBSOCKET_OPCODE_BINARY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Serializer; | ||
|
||
use Thruway\Message\Message; | ||
|
||
interface SerializerInterface | ||
{ | ||
public function serialize(Message $msg): string; | ||
|
||
public function deserialize(string $serializedData): Message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Octamp\Wamp\Serializer; | ||
|
||
interface WampMessageSerializerInterface extends SerializerInterface | ||
{ | ||
public function protocolName(): string; | ||
|
||
public function opcode(): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.