Skip to content

Commit 55e8268

Browse files
committed
Cleanup code and example data
1 parent f6029f8 commit 55e8268

File tree

18 files changed

+161
-82
lines changed

18 files changed

+161
-82
lines changed

src/Application/DemoApplication.php renamed to examples/Application/Chat.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,46 @@
22

33
declare(strict_types=1);
44

5-
namespace Bloatless\WebSocket\Application;
5+
namespace Bloatless\WebSocket\Examples\Application;
66

7+
use Bloatless\WebSocket\Application\Application;
78
use Bloatless\WebSocket\Connection;
89

9-
class DemoApplication extends Application
10+
class Chat extends Application
1011
{
1112
/**
1213
* @var array $clients
1314
*/
1415
private array $clients = [];
1516

17+
/**
18+
* @var array $nicknames
19+
*/
20+
private array $nicknames = [];
21+
1622
/**
1723
* Handles new connections to the application.
1824
*
19-
* @param Connection $client
25+
* @param Connection $connection
2026
* @return void
2127
*/
22-
public function onConnect(Connection $client): void
28+
public function onConnect(Connection $connection): void
2329
{
24-
$id = $client->getClientId();
25-
$this->clients[$id] = $client;
30+
$id = $connection->getClientId();
31+
$this->clients[$id] = $connection;
32+
$this->nicknames[$id] = 'Guest' . rand(10, 999);
2633
}
2734

2835
/**
2936
* Handles client disconnects.
3037
*
31-
* @param Connection $client
38+
* @param Connection $connection
3239
* @return void
3340
*/
34-
public function onDisconnect(Connection $client): void
41+
public function onDisconnect(Connection $connection): void
3542
{
36-
$id = $client->getClientId();
37-
unset($this->clients[$id]);
43+
$id = $connection->getClientId();
44+
unset($this->clients[$id], $this->nicknames[$id]);
3845
}
3946

4047
/**
@@ -49,10 +56,20 @@ public function onData(string $data, Connection $client): void
4956
{
5057
try {
5158
$decodedData = $this->decodeData($data);
52-
$actionName = 'action' . ucfirst($decodedData['action']);
53-
if (method_exists($this, $actionName)) {
54-
call_user_func([$this, $actionName], $decodedData['data']);
59+
60+
// check if action is valid
61+
if ($decodedData['action'] !== 'echo') {
62+
return;
63+
}
64+
65+
$message = $decodedData['data'] ?? '';
66+
if ($message === '') {
67+
return;
5568
}
69+
70+
$clientId = $client->getClientId();
71+
$message = $this->nicknames[$clientId] . ': ' . $message;
72+
$this->actionEcho($message);
5673
} catch (\RuntimeException $e) {
5774
// @todo Handle/Log error
5875
}
@@ -61,8 +78,9 @@ public function onData(string $data, Connection $client): void
6178
public function onIPCData(array $data): void
6279
{
6380
$actionName = 'action' . ucfirst($data['action']);
81+
$message = 'System Message: ' . $data['data'] ?? '';
6482
if (method_exists($this, $actionName)) {
65-
call_user_func([$this, $actionName], $data['data']);
83+
call_user_func([$this, $actionName], $message);
6684
}
6785
}
6886

examples/public/chat.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="css/chat.css">
5+
<script src="js/chat.js"></script>
6+
<meta charset=utf-8 />
7+
<title>Demo Application</title>
8+
</head>
9+
<body>
10+
<div id="container">
11+
<h1>Demo Chat Application</h1>
12+
<span id="status" class="offline">offline</span>
13+
14+
<h2>Server-Response</h2>
15+
<div id="log"></div>
16+
17+
<h2>Send Message</h2>
18+
<input id="data" placeholder="Type a message and hit enter..." type="text" />
19+
<button id="send">Send</button>
20+
</div>
21+
</body>
22+
</html>

public/css/client.css renamed to examples/public/css/chat.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ h2 {
8181
color: #000;
8282
}
8383

84-
#action,
8584
#data {
8685
display: inline-block;
87-
width: 200px;
86+
width: 450px;
8887
margin: 0 0 5px 0;
8988
}
9089
.bold {
File renamed without changes.

public/js/client.js renamed to examples/public/js/chat.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const elLog = document.getElementById('log');
55
const elStatus = document.getElementById('status');
66
const elSend = document.getElementById('send');
7-
const elAction = document.getElementById('action');
87
const elData = document.getElementById('data');
98

109
/**
@@ -17,7 +16,7 @@
1716

1817
// Connect to server
1918
let socket = null;
20-
let serverUrl = 'ws://127.0.0.1:8000/demo';
19+
let serverUrl = 'ws://127.0.0.1:8000/chat';
2120
if (window.MozWebSocket) {
2221
socket = new MozWebSocket(serverUrl);
2322
} else if (window.WebSocket) {
@@ -26,7 +25,7 @@
2625
socket.binaryType = 'blob';
2726

2827
/**
29-
* Callen when connected to websocket server.
28+
* Called when connected to websocket server.
3029
* @param {Object} msg
3130
*/
3231
socket.onopen = (msg) => {
@@ -41,8 +40,7 @@
4140
*/
4241
socket.onmessage = (msg) => {
4342
let response = JSON.parse(msg.data);
44-
log(`Action: ${response.action}`);
45-
log(`Data: ${response.data}`);
43+
log(response.data);
4644
};
4745

4846
/**
@@ -67,10 +65,25 @@
6765
*/
6866
elSend.addEventListener('click', () => {
6967
let payload = {
70-
action: elAction.value,
68+
action: 'echo',
7169
data: elData.value
7270
};
7371
socket.send(JSON.stringify(payload));
7472
});
73+
74+
/**
75+
* Adds event listener to data input, so message can be sent with enter key
76+
*/
77+
elData.addEventListener('keyup', (evt) => {
78+
if (evt.code !== 'Enter') {
79+
return;
80+
}
81+
let payload = {
82+
action: 'echo',
83+
data: elData.value
84+
};
85+
socket.send(JSON.stringify(payload));
86+
elData.value = '';
87+
});
7588
});
7689
}).call();
File renamed without changes.
File renamed without changes.

examples/push.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require __DIR__ . '/../src/PushClient.php';
66

77
$pushClient = new \Bloatless\WebSocket\PushClient('//tmp/phpwss.sock');
8-
$pushClient->sendToApplication('demo', [
8+
$pushClient->sendToApplication('chat', [
99
'action' => 'echo',
1010
'data' => 'Hello from the PushClient!',
1111
]);

examples/server.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
require __DIR__ . '/../src/Application/ApplicationInterface.php';
1111
require __DIR__ . '/../src/Application/Application.php';
12-
require __DIR__ . '/../src/Application/DemoApplication.php';
1312
require __DIR__ . '/../src/Application/StatusApplication.php';
1413

14+
require __DIR__ . '/Application/Chat.php';
15+
1516
$server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000, '/tmp/phpwss.sock');
1617

1718
// server settings:
@@ -22,6 +23,6 @@
2223

2324
// Hint: Status application should not be removed as it displays usefull server informations:
2425
$server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance());
25-
$server->registerApplication('demo', \Bloatless\WebSocket\Application\DemoApplication::getInstance());
26+
$server->registerApplication('chat', \Bloatless\WebSocket\Examples\Application\Chat::getInstance());
2627

2728
$server->run();

public/index.html

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)