diff options
Diffstat (limited to 'assets/php/Chatserver')
-rw-r--r-- | assets/php/Chatserver/bin/WebChatServer.php | 18 | ||||
-rw-r--r-- | assets/php/Chatserver/bin/server.csr | 18 | ||||
-rw-r--r-- | assets/php/Chatserver/src/ChatProcessor.php | 118 |
3 files changed, 154 insertions, 0 deletions
diff --git a/assets/php/Chatserver/bin/WebChatServer.php b/assets/php/Chatserver/bin/WebChatServer.php new file mode 100644 index 0000000..15f573b --- /dev/null +++ b/assets/php/Chatserver/bin/WebChatServer.php @@ -0,0 +1,18 @@ +<?php
+require '../../vendor/autoload.php';
+
+use Ratchet\Server\IoServer;
+use Ratchet\Http\HttpServer;
+use Ratchet\WebSocket\WsServer;
+use Websocket\ChatProcessor;
+
+$server = IoServer::factory(
+ new HttpServer(
+ new WsServer(
+ new ChatProcessor()
+ )
+ ),
+ 1338
+);
+
+$server->run();
\ No newline at end of file diff --git a/assets/php/Chatserver/bin/server.csr b/assets/php/Chatserver/bin/server.csr new file mode 100644 index 0000000..8523b82 --- /dev/null +++ b/assets/php/Chatserver/bin/server.csr @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICzTCCAbUCAQAwgYcxCzAJBgNVBAYTAkRFMRMwEQYDVQQIDApTb21lLVN0YXRl +MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxHjAcBgNVBAMMFW1h +cnZpbmJvcm5lci5kZG5zcy5kZTEgMB4GCSqGSIb3DQEJARYRbWFydmluQGJvcm5l +cnMuZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDjNPmJb5tz9xp+ +h9Jt7Go+VQ31oEu/FINxxW9jamPvvAzcBs82YKoLuOb1NHJX0wWLDAoCwXRn/vpP +pp2OPicl4ACFKOUoKLUj4XXpsFMNsfV5qT4z0rte8P3RYcwbVWlscNekVMbgA6DF +PXvOqZWHgJEZe8UoVP0Pj9J7/xcjY+VpfBhHAEimOipBVxijFAtnOzJ2o1M58xjZ +3XH2BmeSVbjmhSgIbB50zcATKcVudSvaj3hnGmnfwhPRpO2UOvBrrQubwRoTGyvV +TsW6dGGVY/qOJIyBcddayaZJraozBjDwp43c9S44DSotEgeLtvAyceQ27WDl37bl +eEEyMJpvAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEATQ8QnGSJmoudHnYuG8t2 +SHQOxp4R3RcsgXRtXSm3tFlIAvvvixqiqVBfppxK6f6cf4B8tagmXNdblCK8dbWn +9OIBxEh/hrooCtzydXcfvaJaFFaqPQ5YMyNNTtwLHd14w2hSSixNcON+YjfVY2ZA +CdlR/pEDv/xSCABaTNecvSUYBHWsbmo7pMSKBZ/wI0UcgJHbB4PhCjuLTdB79RTp +RJsMzA9ISZd6dLFxkmnOgmq2+sX3xTRK5XSrNfkE9Veou26BHeCwSBA25JVkatqC +DaSuNw3a9SY28Tk1GPjhrKaX8lI40aViWtnuwW36jD3G2GMyXbo4K8hST0wI6gHi +PQ== +-----END CERTIFICATE REQUEST----- diff --git a/assets/php/Chatserver/src/ChatProcessor.php b/assets/php/Chatserver/src/ChatProcessor.php new file mode 100644 index 0000000..da78c9b --- /dev/null +++ b/assets/php/Chatserver/src/ChatProcessor.php @@ -0,0 +1,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();
+ }
+}
\ No newline at end of file |