-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathtcp_server.php
38 lines (32 loc) · 1008 Bytes
/
tcp_server.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
<?php
$serv = new SocketServer();
$serv->run('0.0.0.0', 9504);
class SocketServer
{
protected $serv; //swoole server
const MAX_PACKAGE_LEN = 8000000; //max data accept
function run($host, $port)
{
$this->serv = new Swoole\Server($host, $port, SWOOLE_BASE);
$this->serv->set(array(
'enable_coroutine' => false,
'worker_num' => 1, //how much worker will start
));
$this->serv->on('receive', array($this, 'onReceive'));
$this->serv->start();
}
function onReceive($serv, $fd, $tid, $data)
{
echo "recv " . strlen($data) . " bytes\n";
// $packet = substr($data, 4);
// $result = array(
// "code" => "0",
// "msg" => "ok",
// "data" => $packet,
// );
// $resp = json_encode($result);
// $send_data = pack('N', strlen($resp)) . $resp;
// echo "send " . strlen($send_data) . " bytes\n";
// $serv->send($fd, $send_data);
}
}