-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
75 lines (62 loc) · 2.1 KB
/
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
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
<?php
//官网demo
$server = new swoole_websocket_server("0.0.0.0", 1997);
$server->on('open', function (swoole_websocket_server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";//$request->fd 是客户端id
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
//$frame->fd 是客户端id,$frame->data是客户端发送的数据
//服务端向客户端发送数据是用 $server->push( '客户端id' , '内容')
$data = $frame->data;
foreach($server->connections as $fd){
$server->push($fd , $data);//循环广播
}
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
/*$serv = new swoole_websocket_server("0.0.0.0", 1997);
$serv->on('Open', function($server, $req) {
echo "connection open: ".$req->fd;
});
$serv->on('Message', function($server, $frame) {
echo time()." receive message: ".$frame->data."\n";
// $server->push($frame->fd,$frame->data);
/* foreach ($server->connection as $fd) {
if ($frame->fd!=$fd) {
$server->send($fd,$frame->data);
}
}
});
$serv->on('Close', function($server, $fd) {
echo "$fd connection close: ".$fd;
});
$serv->start();*/
/*$serv = new swoole_server("0.0.0.0", 1234);
$serv->on('connect', function ($serv, $fd){
echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, 'Swoole: '.$data);
$serv->close($fd);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();*/
/*$serv = new swoole_http_server("127.0.0.1", 9502);
$serv->on('Request', function($request, $response) {
var_dump($request->get);
var_dump($request->post);
var_dump($request->cookie);
var_dump($request->files);
var_dump($request->header);
var_dump($request->server);
$response->cookie("User", "Swoole");
$response->header("X-Server", "Swoole");
$response->end("<h1>Hello Swoole!</h1>");
});
$serv->start();*/
?>