This commit is contained in:
Enoch
2024-11-10 21:16:01 +08:00
commit eec520f969
157 changed files with 15607 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
class Binary extends Message
{
protected $opcode = 'binary';
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
class Close extends Message
{
protected $opcode = 'close';
}
+32
View File
@@ -0,0 +1,32 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
use WebSocket\BadOpcodeException;
class Factory
{
public function create(string $opcode, string $payload = ''): Message
{
switch ($opcode) {
case 'text':
return new Text($payload);
case 'binary':
return new Binary($payload);
case 'ping':
return new Ping($payload);
case 'pong':
return new Pong($payload);
case 'close':
return new Close($payload);
}
throw new BadOpcodeException("Invalid opcode '{$opcode}' provided");
}
}
+74
View File
@@ -0,0 +1,74 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
use DateTime;
abstract class Message
{
protected $opcode;
protected $payload;
protected $timestamp;
public function __construct(string $payload = '')
{
$this->payload = $payload;
$this->timestamp = new DateTime();
}
public function getOpcode(): string
{
return $this->opcode;
}
public function getLength(): int
{
return strlen($this->payload);
}
public function getTimestamp(): DateTime
{
return $this->timestamp;
}
public function getContent(): string
{
return $this->payload;
}
public function setContent(string $payload = ''): void
{
$this->payload = $payload;
}
public function hasContent(): bool
{
return $this->payload != '';
}
public function __toString(): string
{
return get_class($this);
}
// Split messages into frames
public function getFrames(bool $masked = true, int $framesize = 4096): array
{
$frames = [];
$split = str_split($this->getContent(), $framesize) ?: [''];
foreach ($split as $payload) {
$frames[] = [false, $payload, 'continuation', $masked];
}
$frames[0][2] = $this->opcode;
$frames[array_key_last($frames)][0] = true;
return $frames;
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
class Ping extends Message
{
protected $opcode = 'ping';
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
class Pong extends Message
{
protected $opcode = 'pong';
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* Copyright (C) 2014-2022 Textalk/Abicart and contributors.
*
* This file is part of Websocket PHP and is free software under the ISC License.
* License text: https://raw.githubusercontent.com/Textalk/websocket-php/master/COPYING
*/
namespace WebSocket\Message;
class Text extends Message
{
protected $opcode = 'text';
}