Skip to content

Commit e620a74

Browse files
committed
Add basic architecture for IPC socket
1 parent 58afa2c commit e620a74

File tree

11 files changed

+201
-22
lines changed

11 files changed

+201
-22
lines changed

cli/client_demo.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
/*
23
ini_set('display_errors', 1);
34
error_reporting(E_ALL);
45
@@ -24,3 +25,19 @@
2425
usleep(5000);
2526
}
2627
usleep(5000);
28+
*/
29+
30+
31+
require __DIR__ . '/../src/IPCPayloadFactory.php';
32+
require __DIR__ . '/../src/IPCPayload.php';
33+
34+
$payload = \Bloatless\WebSocket\IPCPayloadFactory::makeApplicationPayload('demo', [
35+
'action' => 'echo',
36+
'data' => 'Hello from the IPC Socket!',
37+
]);
38+
39+
$dataToSend = $payload->asJson();
40+
$dataLength = strlen($dataToSend);
41+
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
42+
socket_sendto($socket, $dataToSend, $dataLength, MSG_EOF, '/tmp/phpwss.sock', 0);
43+
socket_close($socket);

cli/server.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
require __DIR__ . '/../src/Connection.php';
4+
require __DIR__ . '/../src/IPCPayload.php';
5+
require __DIR__ . '/../src/IPCPayloadFactory.php';
46
require __DIR__ . '/../src/Server.php';
57
require __DIR__ . '/../src/Timer.php';
68
require __DIR__ . '/../src/TimerCollection.php';
@@ -10,7 +12,7 @@
1012
require __DIR__ . '/../src/Application/DemoApplication.php';
1113
require __DIR__ . '/../src/Application/StatusApplication.php';
1214

13-
$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000);
15+
$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000, '/tmp/phpwss.sock');
1416

1517
// server settings:
1618
$server->setMaxClients(100);

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
}
2222
},
2323
"require": {
24-
"php": "^7.2"
24+
"php": "^7.4|^8.0",
25+
"ext-json": "*",
26+
"ext-sockets": "*"
2527
}
2628
}

public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<link rel="stylesheet" href="css/client.css">
55
<script src="js/client.js"></script>
66
<meta charset=utf-8 />
7-
<title>Shiny WSS Demo Application</title>
7+
<title>Demo Application</title>
88
</head>
99
<body>
1010
<div id="container">
11-
<h1>Shiny WSS Demo Application</h1>
11+
<h1>Demo Application</h1>
1212
<span id="status" class="offline">offline</span>
1313

1414
<h2>Send Text Frame</h2>
@@ -20,4 +20,4 @@ <h2>Server-Response</h2>
2020
<div id="log"></div>
2121
</div>
2222
</body>
23-
</html>
23+
</html>

public/status.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<link rel="stylesheet" href="css/status.css">
55
<script src="js/status.js"></script>
66
<meta charset=utf-8 />
7-
<title>Shiny WSS Status</title>
7+
<title>Server Status</title>
88
</head>
99
<body>
1010
<div id="container">
11-
<h1>Shiny WSS Status</h1>
11+
<h1>Server Status</h1>
1212
<span id="status" class="offline">disconnected</span>
1313

1414
<div id="main">
@@ -33,4 +33,4 @@ <h2>Server Messages:</h2>
3333
</div>
3434
</div>
3535
</body>
36-
</html>
36+
</html>

src/Application/ApplicationInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bloatless\WebSocket\Application;
66

77
use Bloatless\WebSocket\Connection;
8+
use Bloatless\WebSocket\IPCPayload;
89

910
interface ApplicationInterface
1011
{
@@ -23,13 +24,20 @@ public function onConnect(Connection $connection): void;
2324
public function onDisconnect(Connection $connection): void;
2425

2526
/**
26-
* This method is triggered when the server recieves new data.
27+
* This method is triggered when the server recieves new data from a client.
2728
*
2829
* @param string $data
2930
* @param Connection $client
3031
*/
3132
public function onData(string $data, Connection $client): void;
3233

34+
/**
35+
* This method is called when server recieves to for an application on the IPC socket.
36+
*
37+
* @param array $data
38+
*/
39+
public function onIPCData(array $data): void;
40+
3341
/**
3442
* Creates and returns a new instance of the application.
3543
*

src/Application/DemoApplication.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public function onData(string $data, Connection $client): void
5858
}
5959
}
6060

61+
public function onIPCData(array $data): void
62+
{
63+
$actionName = 'action' . ucfirst($data['action']);
64+
if (method_exists($this, $actionName)) {
65+
call_user_func([$this, $actionName], $data['data']);
66+
}
67+
}
68+
6169
/**
6270
* Echoes data back to client(s).
6371
*

src/Application/StatusApplication.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function onData(string $data, Connection $client): void
7373
// currently not in use...
7474
}
7575

76+
public function onIPCData(array $data): void
77+
{
78+
// TODO: Implement onIPCData() method.
79+
}
80+
7681
/**
7782
* Sets basic server data.
7883
*

src/IPCPayload.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bloatless\WebSocket;
6+
7+
class IPCPayload
8+
{
9+
public const TYPE_SERVER = 1;
10+
11+
public const TYPE_APPLICATION = 2;
12+
13+
public int $type;
14+
15+
public string $action;
16+
17+
public array $data;
18+
19+
public function __construct(int $type, string $action, array $data = [])
20+
{
21+
$this->type = $type;
22+
$this->action = $action;
23+
$this->data = $data;
24+
}
25+
26+
public function asJson(): string
27+
{
28+
return json_encode([
29+
'type' => $this->type,
30+
'action' => $this->action,
31+
'data' => $this->data,
32+
]);
33+
}
34+
}

src/IPCPayloadFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bloatless\WebSocket;
6+
7+
class IPCPayloadFactory
8+
{
9+
public static function makeServerPayload(string $command, array $data = []): IPCPayload
10+
{
11+
return new IPCPayload(IPCPayload::TYPE_SERVER, $command, $data);
12+
}
13+
14+
public static function makeApplicationPayload(string $applicationName, array $data): IPCPayload
15+
{
16+
return new IPCPayload(IPCPayload::TYPE_APPLICATION, $applicationName, $data);
17+
}
18+
19+
public static function fromJson(string $json): IPCPayload
20+
{
21+
$data = json_decode($json, true);
22+
switch ($data['type']) {
23+
case IPCPayload::TYPE_SERVER:
24+
return new IPCPayload(IPCPayload::TYPE_SERVER, $data['action'], $data['data']);
25+
case IPCPayload::TYPE_APPLICATION:
26+
return new IPCPayload(IPCPayload::TYPE_APPLICATION, $data['action'], $data['data']);
27+
default:
28+
throw new \RuntimeException('Can not create IPCPayload from invalid data type.');
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)