Skip to content

Commit 6b25bac

Browse files
committed
Add comments and cleanup code
1 parent 72e8ad4 commit 6b25bac

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

index.html

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
2-
<html lang="fr" dir="ltr">
2+
<html lang="en" dir="ltr">
33
<head>
44
<meta charset="utf-8" />
5-
<title>Chat</title>
5+
<title>Chat - Client</title>
66
</head>
77
<body>
88
<!-- Form to send messages -->
@@ -15,19 +15,19 @@
1515
<ul id="chat"></ul>
1616

1717
<script type="text/javascript">
18+
const FORM_ID = 'msg_form'
19+
const CHAT_ID = 'chat'
20+
const SOCKET_HOST = 'ws://localhost:8080'
21+
1822
// Manage socket
19-
const socket = new WebSocket('ws://localhost:8080');
20-
socket.onopen = function(e) {
21-
const target = e.target
22-
console.log('Connected to ' + target.url);
23-
}
24-
socket.onmessage = function(e) {
25-
const msg = e.data
26-
addMessage(msg)
27-
}
23+
const socket = new WebSocket(SOCKET_HOST);
24+
// The socket is open
25+
socket.onopen = (e) => console.log('Connected to ' + e.target.url)
26+
// The client get a message
27+
socket.onmessage = (e) => addMessage(e.data)
2828

2929
// Send message from the form
30-
document.getElementById('msg_form').onsubmit = (e) => {
30+
document.getElementById(FORM_ID).onsubmit = (e) => {
3131
e.preventDefault()
3232
const form = e.target
3333
const msg = form[0].value
@@ -36,11 +36,14 @@
3636
socket.send(msg)
3737
}
3838

39-
// Add a message to the chat
39+
/**
40+
* Add a message to the chat
41+
* @param {String} msg The message to add
42+
*/
4043
function addMessage(msg){
4144
const list_node = document.createElement('LI')
4245
list_node.innerText = msg
43-
document.getElementById('chat').appendChild(list_node)
46+
document.getElementById(CHAT_ID).appendChild(list_node)
4447
}
4548
</script>
4649
</body>

server.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
require __DIR__.'/vendor/autoload.php';
44

5+
// Include Ratchet
56
use Ratchet\Server\IoServer;
67
use Ratchet\Http\HttpServer;
78
use Ratchet\WebSocket\WsServer;
89
use Ratchet\MessageComponentInterface;
910
use Ratchet\ConnectionInterface;
1011

12+
// Define the app port
1113
define('APP_PORT', 8080);
1214

1315
class ServerImpl implements MessageComponentInterface {
@@ -17,39 +19,52 @@ public function __construct() {
1719
$this->clients = new \SplObjectStorage;
1820
}
1921

22+
/**
23+
* A new connection is on
24+
* @param ConnectionInterface $conn Connection data
25+
*/
2026
public function onOpen(ConnectionInterface $conn) {
2127
$this->clients->attach($conn);
22-
echo "New connection! ({$conn->resourceId}).\n";
28+
echo "New connection : {$conn->resourceId}\n";
2329
}
2430

31+
/**
32+
* A message is received
33+
* @param ConnectionInterface $conn Sender connection data
34+
* @param String $msg The received message
35+
*/
2536
public function onMessage(ConnectionInterface $conn, $msg) {
26-
echo sprintf("New message from '%s': %s\n\n\n", $conn->resourceId, $msg);
27-
foreach ($this->clients as $client) { // BROADCAST
37+
echo sprintf("New message from '%s': %s\n", $conn->resourceId, $msg);
38+
// Send the received message to all connected clients
39+
foreach ($this->clients as $client) {
2840
$message = json_decode($msg, true);
2941
if ($conn !== $client) {
3042
$client->send($msg);
3143
}
3244
}
3345
}
3446

47+
/**
48+
* A connection is over
49+
* @param ConnectionInterface $conn Connection data
50+
*/
3551
public function onClose(ConnectionInterface $conn) {
3652
$this->clients->detach($conn);
37-
echo "Connection {$conn->resourceId} is gone.\n";
53+
echo "Connection to {$conn->resourceId} is over.\n";
3854
}
3955

56+
/**
57+
* An error is occured
58+
* @param ConnectionInterface $conn Involved connection data
59+
* @param Exception $e Error data
60+
*/
4061
public function onError(ConnectionInterface $conn, \Exception $e) {
41-
echo "An error occured on connection {$conn->resourceId}: {$e->getMessage()}\n\n\n";
62+
echo "An error occured on connection {$conn->resourceId}: {$e->getMessage()}\n\n";
4263
$conn->close();
4364
}
4465
}
4566

46-
$server = IoServer::factory(
47-
new HttpServer(
48-
new WsServer(
49-
new ServerImpl()
50-
)
51-
),
52-
APP_PORT
53-
);
54-
echo "Server created on port " . APP_PORT . "\n\n";
67+
// Create and run the server
68+
$server = IoServer::factory(new HttpServer(new WsServer(new ServerImpl())), APP_PORT);
69+
echo "Server is running on port " . APP_PORT . "...\n";
5570
$server->run();

0 commit comments

Comments
 (0)