Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostzero committed Mar 17, 2021
0 parents commit 5824398
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
18 changes: 18 additions & 0 deletions README.md
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')
],
```
39 changes: 39 additions & 0 deletions composer.json
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"
]
}
}
71 changes: 71 additions & 0 deletions src/Own3d/Socket/SocketClient.php
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;
}
}
31 changes: 31 additions & 0 deletions tests/SocketClientTest.php
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',
],
],
]);
}
}
12 changes: 12 additions & 0 deletions tests/TestCase.php
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
{
//
}

0 comments on commit 5824398

Please sign in to comment.