1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Examples |
8
|
|
|
* @subpackage WebSocket |
9
|
|
|
* |
10
|
|
|
* @author Vasily Zorin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ExampleWebSocket extends \PHPDaemon\Core\AppInstance |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Called when the worker is ready to go. |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function onReady() |
19
|
|
|
{ |
20
|
|
|
$appInstance = $this; // a reference to this application instance for ExampleWebSocketRoute |
21
|
|
|
// URI /exampleApp should be handled by ExampleWebSocketRoute |
22
|
|
|
\PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('exampleApp', function ($client) use ($appInstance) { |
23
|
|
|
return new ExampleWebSocketRoute($client, $appInstance); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates Request. |
29
|
|
|
* @param object Request. |
30
|
|
|
* @param object Upstream application instance. |
31
|
|
|
* @return ExampleWebSocketTestPageRequest Request. |
32
|
|
|
*/ |
33
|
|
|
public function beginRequest($req, $upstream) |
34
|
|
|
{ |
35
|
|
|
return new ExampleWebSocketTestPageRequest($this, $upstream, $req); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
class ExampleWebSocketRoute extends \PHPDaemon\WebSocket\Route |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Called when new frame received. |
44
|
|
|
* @param string Frame's contents. |
45
|
|
|
* @param integer Frame's type. |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function onFrame($data, $type) |
49
|
|
|
{ |
50
|
|
|
if ($data === 'ping') { |
51
|
|
|
$this->client->sendFrame('pong', 'STRING', |
52
|
|
|
function ($client) { // optional. called when the frame is transmitted to the client |
|
|
|
|
53
|
|
|
\PHPDaemon\Core\Daemon::log('ExampleWebSocket: \'pong\' received by client.'); |
54
|
|
|
} |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Uncaught exception handler |
61
|
|
|
* @param $e |
62
|
|
|
* @return boolean|null Handled? |
63
|
|
|
*/ |
64
|
|
|
public function handleException($e) |
65
|
|
|
{ |
66
|
|
|
$this->client->sendFrame('exception ...'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
class ExampleWebSocketTestPageRequest extends Generic |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Called when request iterated. |
75
|
|
|
* @return integer Status. |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
public function run() |
78
|
|
|
{ |
79
|
|
|
$this->header('Content-Type: text/html'); |
80
|
|
|
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
81
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
82
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
83
|
|
|
<head> |
84
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
85
|
|
|
<title>WebSocket test page</title> |
86
|
|
|
</head> |
87
|
|
|
<body> |
88
|
|
|
<script> |
89
|
|
|
function create() { |
90
|
|
|
// Example |
91
|
|
|
ws = new WebSocket('ws://' + document.domain + ':8047/exampleApp'); |
92
|
|
|
ws.onopen = function () { |
93
|
|
|
document.getElementById('log').innerHTML += 'WebSocket opened <br/>'; |
94
|
|
|
} |
95
|
|
|
ws.onmessage = function (e) { |
96
|
|
|
document.getElementById('log').innerHTML += 'WebSocket message: ' + e.data + ' <br/>'; |
97
|
|
|
} |
98
|
|
|
ws.onclose = function () { |
99
|
|
|
document.getElementById('log').innerHTML += 'WebSocket closed <br/>'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
</script> |
103
|
|
|
<button onclick="create();">Create WebSocket</button> |
104
|
|
|
<button onclick="ws.send('ping');">Send ping</button> |
105
|
|
|
<button onclick="ws.close();">Close WebSocket</button> |
106
|
|
|
<div id="log" style="width:300px; height: 300px; border: 1px solid #999999; overflow:auto;"></div> |
107
|
|
|
</body> |
108
|
|
|
</html> |
109
|
|
|
<?php |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.