forked from mehmetKadiOglu/MongoDbMySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketim.php
72 lines (60 loc) · 1.64 KB
/
socketim.php
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
<?php
set_time_limit(0);
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require_once '../vendor/autoload.php';
class Chat implements MessageComponentInterface {
protected $clients;
protected $users;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
// $this->users[$conn->resourceId] = $conn;
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
// unset($this->users[$conn->resourceId]);
}
public function onMessage(ConnectionInterface $from, $data) {
$data = json_decode($data);
$sendData["type"] = $data->type;
if(isset($data->anahtar)){
$sendData["yazanKullanici"] = $data->kullanici;
$sendData["metin"] = $data->metin;
$sendData["tarih"] = $data->tarih;
$sendData["anahtar"] = $data->anahtar;
}
// php.exe sockeim.php
switch ($sendData["type"]) {
case 'konu':
$sendData["konu"] = $data->konu;
break;
case 'konuSil':
$sendData["konuKey"] = $data->konuKey;
break;
case 'yorumSil':
$sendData["konuKey"] = $data->konuKey;
$sendData["yorumKey"] = $data->yorumKey;
break;
}
$this->datayiGonder($sendData);
}
private function datayiGonder($sendData){
foreach($this->clients as $client)
$client->send(json_encode($sendData));
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(new WsServer(new Chat())),
8080
);
$server->run();
?>