aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/Chatserver/src/ChatProcessor.php
blob: da78c9bc34326f086b283e25052d3587bdcb61f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

namespace Websocket;

//use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\WebSocket\MessageComponentInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Nubs\RandomNameGenerator\Alliteration;

class ChatProcessor implements MessageComponentInterface
{
    protected $clients;
    private $subscriptions;
    private $users;
    private $connectedUsersNames;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
        $this->subscriptions = [];
        $this->users = [];
        $this->connectedUsersNames = [];
    }

    public function onOpen(ConnectionInterface $conn) {
        $generator = new Alliteration();
        $this->clients->attach($conn);
        $this->users[$conn->resourceId] = $conn;
        $this->connectedUsersNames[$conn->resourceId] = $generator->getName();
    }

    /*public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from === $client) {
                $client->send("<b>You</b> - " . $msg);
            } else {
                $client->send("<b>" . $from->resourceId . "</b> - " . $msg);
            }
        }
    }
    */

    public function onMessage(ConnectionInterface $conn, MessageInterface $msg) {
        $data = json_decode($msg);
        switch ($data->ClientMessageType) {
            case "Subscribe":
                $this->subscriptions[$conn->resourceId] = $data->Channel;
                foreach ($this->subscriptions as $id => $channel) {
                    if ($this->subscriptions[$conn->resourceId] == $channel) {
                        $MessageObject = new \stdClass();
                        $MessageObject->ServerMessage = true;
                        $MessageObject->ServerMessageType = "GroupJoin";
                        $MessageObject->GroupName = $channel;
                        $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
                        if ($id === $conn->resourceId) {
                            $MessageObject->WasHimself = true;
                        } else {
                            $MessageObject->WasHimself = false;
                        }
                        $MessageJson = json_encode($MessageObject, true);
                        $this->users[$id]->send($MessageJson);
                    }
                }
                break;
            case "Message":
                if (isset($this->subscriptions[$conn->resourceId])) {
                    $target = $this->subscriptions[$conn->resourceId];
                    foreach ($this->subscriptions as $id => $channel) {
                        if ($channel == $target) {
                            $MessageObject = new \stdClass();
                            $MessageObject->ServerMessage = false;
                            $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
                            $MessageObject->Message = htmlspecialchars($data->Message);
                            if ($id === $conn->resourceId) {
                                $MessageObject->WasHimself = true;
                            } else {
                                $MessageObject->WasHimself = false;
                            }
                            $MessageJson = json_encode($MessageObject, true);
                            $this->users[$id]->send($MessageJson);
                        }
                    }
                }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
        foreach ($this->clients as $client) {
            if (isset($this->subscriptions[$conn->resourceId])) {
                $target = $this->subscriptions[$conn->resourceId];
                foreach ($this->subscriptions as $id => $channel) {
                    if ($channel == $target) {
                        $MessageObject = new \stdClass();
                        $MessageObject->ServerMessage = true;
                        $MessageObject->ServerMessageType = "UserDisconnect";
                        $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
                        $MessageJson = json_encode($MessageObject, true);
                        $this->users[$id]->send($MessageJson);
                    }
                }
            }
        }
        unset($this->users[$conn->resourceId]);
        unset($this->subscriptions[$conn->resourceId]);
        unset($this->connectedUsersNames[$conn->resourceId]);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}