-
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.
- Loading branch information
0 parents
commit 5824398
Showing
6 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
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,18 @@ | ||
# OWN3D Socket | ||
|
||
## Installation | ||
|
||
```bash | ||
composer require own3d/socket | ||
``` | ||
|
||
## Configuration | ||
|
||
Add the `own3d-socket` configuration within the `config/services.php` config file. | ||
|
||
```php | ||
'own3d-socket' => [ | ||
'username' => env('OWN3D_SOCKET_USERNAME', 'own3d-socket'), | ||
'password' => env('OWN3D_SOCKET_PASSWORD') | ||
], | ||
``` |
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,39 @@ | ||
{ | ||
"name": "own3d/socket", | ||
"description": "OWN3D Socket Client", | ||
"type": "library", | ||
"license": "proprietary", | ||
"authors": [ | ||
{ | ||
"name": "René Preuß", | ||
"email": "rene.p@own3d.tv" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4|^8.0", | ||
"guzzlehttp/guzzle": "^6.3|^7.0", | ||
"own3d/id": "^1.7" | ||
}, | ||
"require-dev": { | ||
"own3d/php-cs-fixer-config": "^1.0", | ||
"friendsofphp/php-cs-fixer": "^2.18", | ||
"phpunit/phpunit": "^9.5", | ||
"orchestra/testbench": "^6.14" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Own3d\\Socket\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Own3d\\Socket\\Tests\\": "tests" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "vendor/bin/phpunit", | ||
"fix": [ | ||
"vendor/bin/php-cs-fixer fix" | ||
] | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
namespace Own3d\Socket; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\RequestOptions; | ||
use Throwable; | ||
|
||
class SocketClient | ||
{ | ||
public static string $baseUrl = 'https://socket-hel1-1.own3d.dev'; | ||
|
||
private Client $client; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = new Client([ | ||
'base_uri' => self::$baseUrl, | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $baseUrl | ||
* | ||
* @internal only for internal and debug purposes | ||
*/ | ||
public static function setBaseUrl(string $baseUrl): void | ||
{ | ||
self::$baseUrl = $baseUrl; | ||
} | ||
|
||
/** | ||
* Sends a message into the own3d websocket system. | ||
* | ||
* Required parameters: | ||
* - room: string | ||
* - event: string | ||
* - data: array | ||
* | ||
* @param array $parameters | ||
* @param array|null $serverId The server id is the hostname, that should receive the message (optional). | ||
* @return bool | ||
*/ | ||
public function emit(array $parameters, array $serverId = null): bool | ||
{ | ||
$options = [ | ||
RequestOptions::JSON => $parameters, | ||
RequestOptions::AUTH => [ | ||
config('services.own3d-socket.username'), | ||
config('services.own3d-socket.password'), | ||
], | ||
]; | ||
|
||
if ($serverId) { | ||
$options[RequestOptions::HEADERS]['cookie'] = sprintf('serverid=%s', $serverId); | ||
} | ||
|
||
try { | ||
$response = $this->getClient()->post('emit', $options); | ||
} catch (Throwable $exception) { | ||
return false; | ||
} | ||
|
||
return in_array($response->getStatusCode(), [200, 204]); | ||
} | ||
|
||
public function getClient(): Client | ||
{ | ||
return $this->client; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace Own3d\Socket\Tests; | ||
|
||
use Own3d\Socket\SocketClient; | ||
|
||
class SocketClientTest extends TestCase | ||
{ | ||
public function testSubscribe(): void | ||
{ | ||
$socketClient = new SocketClient(); | ||
|
||
$socketClient->emit([ | ||
'room' => 'twitch.106415581', | ||
'event' => 'notifysub', | ||
'data' => [ | ||
'subscription' => [ | ||
'type' => 'follow', | ||
'version' => '1', | ||
'condition' => [ | ||
'platform' => 'twitch', | ||
'platform_id' => '106415581', | ||
] | ||
], | ||
'event' => [ | ||
'name' => 'OWN3D', | ||
], | ||
], | ||
]); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Own3d\Socket\Tests; | ||
|
||
use Orchestra\Testbench\TestCase as BaseTestCase; | ||
|
||
class TestCase extends BaseTestCase | ||
{ | ||
// | ||
} |