forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDCServer.php
More file actions
88 lines (72 loc) · 2.33 KB
/
PDCServer.php
File metadata and controls
88 lines (72 loc) · 2.33 KB
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
<?php
namespace PocketDockConsole;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Terminal;
ini_set('display_errors', 1);
error_reporting(E_ALL);
class PDCServer extends \pocketmine\Thread {
public $null = NULL;
public $buffer = "";
public $stuffToSend = "";
public $jsonStream = "";
public $stuffTitle = "";
public $stop = false;
public function __construct($host, $port, $logger, $loader, $password, $html, $backlog, $legacy = false) {
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->logger = $logger;
$this->loader = $loader;
$this->data = $html;
$this->backlog = $backlog;
$this->clienttokill = "";
$this->sendUpate = false;;
$this->legacy = $legacy;
$this->start();
$this->log("Started SocksServer on " . $this->host . ":" . $this->port);
}
public function getBuffer() {
return $this->buffer;
}
public function run() {
$this->registerClassLoader();
set_exception_handler(function ($ex) {
//var_dump($ex);
$this->logger->debug($ex->getMessage());
});
if (!$this->legacy) {
Terminal::init();
}
$server = new \Wrench\Server('ws://' . $this->host . ':' . $this->port, array("logger" => function ($msg, $pri) {
}), $this);
$server->registerApplication("app", new PDCApp($this, $this->password));
$server->addListener(\Wrench\Server::EVENT_SOCKET_CONNECT, function ($data, $other) {
$header = $other->getSocket()->receive();
if ($this->isHTTP($header)) {
$other->getSocket()->send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" . $this->data);
$other->close(200);
} else {
$other->onData($header);
}
});
while($this->stop !== true) {
try {
$server->run();
} catch (\Exception $e) {
}
}
}
public function isHTTP($data) {
if (strpos($data, "websocket")) {
return false;
} else {
return true;
}
}
public function stop() {
$this->stop = true;
}
public function log($data) {
$this->logger->info("[PDC] " . $data);
}
}