一、建立tcp服务器
//服务器端ip : 127.0.0.1
$serv = new swoole_server('127.0.0.1',9501);
$serv->on('connect',function($serv,$fd){
echo "建立连接";
});
$serv->on('receive',function($serv,$fd,$from_id,$data){
echo "接收到数据\n";
});
$serv->on('close',function($serv,$fd){
echo "连接关闭";
});
$serv->start(); //启动服务器
图片.png
图片.png
二、建立udp服务器
$serv = new swoole_server("0.0.0.0",9502,SWOOLE_PROCESS,SWOOLE_SOCK_UDP);
//监听数据接收的事件
/**
* $serv: 服务器信息
* $data: 数据,接收到的数据
* $fd: 客户端信息
*/
$serv->on('packet',function($serv,$data,$fd){
//发送数据到相应客户端,反馈信息
$serv->sendto($fd['address'],$fd['port'],"Server:$data");
var_dump($fd);
});
$serv->start(); //启动服务
图片.png
三、建立web服务器
$serv = new swoole_http_server("0.0.0.0",9503);
/**
* $request 请求信息 get|post
* $response 返回信息
*/
$serv->on('request',function($request,$response){
var_dump($request);
$response->header("Content-Type","text/html;charset=utf-8");
$response->end("hello world".rand(100,999));
});
$serv->start();
四、 websocket服务器
是建立在http服务器之上的长连接服务器
//创建websocket服务器对象,监听0.0.0.0:9504端口
$ws = new swoole_websocket_server("0.0.0.0",9504);
//监听websocket连接打开事件
//建立连接 $ws 服务器 $request 客户端信息
$ws->on('open',function($ws,$request){
var_dump($request->fd,$request->get,$request->server);
$ws->push($request->fd,"hello,welcome\n");
});
//监听websocket消息事件
//接收信息
$ws->on('message',function($ws,$frame){
echo "Message:{$frame->data}\n";
$ws->push($frame->fd,"server: {$frame->data}");
});
//监听websocket连接关闭事件
$ws->on('close',function($ws,$fd){
echo "client-{$fd} is close\n";
});
$ws->start();
1、直接访问
图片.png
2、js访问
//创建websocket服务器对象,监听0.0.0.0:9504端口
$ws = new swoole_websocket_server("0.0.0.0",9504);
//监听websocket连接打开事件
//建立连接 $ws 服务器 $request 客户端信息
$ws->on('open',function($ws,$request){
var_dump($request->fd,$request->get,$request->server);
$ws->push($request->fd,"hello,welcome\n");
});
//监听websocket消息事件
//接收信息
$ws->on('message',function($ws,$frame){
echo "Message:{$frame->data}\n";
$ws->push($frame->fd,"server: {$frame->data}");
});
//监听websocket连接关闭事件
$ws->on('close',function($ws,$fd){
echo "client-{$fd} is close\n";
});
$ws->start();
<script>
var wsServer = "ws://127.0.0.1:9504";
var webSocket = new WebSocket(wsServer);
webSocket.onopen = function (evt) {
console.log("连接成功");
}
webSocket.onclose = function (evt) {
console.log("关闭");
}
webSocket.onmessage = function(evt){
console.log(evt.data);
}
webSocket.onerror = function (evt,e) {
console.log("error");
}
</script>
图片.png
五、异步tcp服务器
//创建tcp服务器
$serv = new swoole_server("0.0.0.0",9505);
//设置异步,进程工作数
$serv->set(['task_worker_num'=>4]);
//投递异步任务
$serv->on("receive",function($serv,$fd,$from_id,$data){
$task_id = $serv->task($data); //异步ID
echo "异步ID:$task_id \n";
});
//处理异步任务
$serv->on('task',function ($serv,$task_id,$from_id,$data){
echo "执行 异步ID:$task_id";
$serv->finish("$data -> OK");
});
//处理结果
$serv->on('finish',function($serv,$task_id,$data){
echo "执行完成";
});
$serv->start();
图片.png
六、tcp客户端
$client = new swoole_client(SWOOLE_SOCK_TCP);
$client->connect("192.168.1.107",80,5) or die("连接失败");
//向服务器发送数据
$client->send("hello world") or die("数据发送失败");
//接收数据
$data = $client->recv();
if(!$data)
{
die("recv failed.");
}
echo $data;
//关闭连接
$client->close();
网友评论