swoole1--搭建echo服务器
1.安装swoole :pecl install swoole,然后修改php.ini 开启swoole扩展:extension=swoole.so
2.写一个服务器Server.php
1 <?php
2
3 class Server{
4 private $serv;
5
6 public function __construct(){
7 $this->serv = new swoole_server("0.0.0.0",9501);
8 $this->serv->set(['worker_num'=>8,'daemonize'=>false]);
//注册服务器回调时间
9 $this->serv->on('Start',[$this,'onStart']);
10 $this->serv->on('Connect',[$this,'onConnect']);
11 $this->serv->on('Receive',[$this,'onReceive']);
12 $this->serv->on('Close',[$this,'onClose']);
13 $this->serv->start();
14 }
15
16 public function onStart($serv){
17 echo "Start\n";
18 }
19
20 public function onConnect($serv,$fd,$from_id){
21 $serv->send($fd,"hello,$fd,欢迎连接");
22 }
23
24 public function onReceive($serv,$fd,$from_id,$data){
25 echo "get message from client {$fd},{$data}\n";
26 $serv->send($fd,$data);
27
28 }
29
30 public function onClose($serv,$fd,$from_id){
31 echo "client {$fd} close connection \n";
32 }
33
34 }
35
36 @$server = new Server();//开了xDebug,这里不用@会弹出警告信息
3.写一个客户端Client.php
<?php
class Client
{
private $client; public function __construct() {
$this->client = new swoole_client(SWOOLE_SOCK_TCP);//同步tcp客户端
} public function connect() {
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
echo "Error: {$this->client->errMsg}[{$this->client->errCode}]\n";
} fwrite(STDOUT, "请输入消息 Please input msg:"); //STDOUT,STDIN : php的伪协议,标准输入(php://stdin)输出(php://stdout)流,这里是将提示信息输出到命令行,类似echo
$msg = trim(fgets(STDIN));//在这里fgets会读取命令行输入的内容,直到读取到换行符或者、EOF或者最大数据量的时候停止读取
$this->client->send( $msg ); $message = $this->client->recv();
echo "Get Message From Server:{$message}\n";
}
} $client = new Client();
$client->connect();//这里只能收发一次消息后关闭
4.异步客户端
1 <?php
2
3 class Client{
4 private $client;
5
6 public function __construct(){
7 $this->client = new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC);//加了异步之后要注册回调,不然报错
8 $this->client->on('connect',[$this,'onConnect']);
9 $this->client->on('receive',[$this,'onReceive']);
10 $this->client->on('error',[$this,'onError']);
11 $this->client->on('close',[$this,'onClose']);
12 }
13
14 public function connect(){
15 if(!$this->client->connect("127.0.0.1",9501,1)){
16 echo "Error :{$this->client->errMsg}[{$this->client->errCode}]\n";
17 }
18 //客户端异步之后就不能再同步发消息
19 // fwrite(STDOUT,"请输入消息(please input msg):");
20 // $msg = trim(fgets(STDIN));
21 // $this->client->send($msg);
22
23 // $message = $this->client->recv();
24 // echo "get message from Server:{$message}\n";
25 }
26
27 public function onConnect($cli){
28 $cli->send("hello server ,I'm connect".PHP_EOL);
29 }
30
31 public function onReceive($cli,$data){
32 echo "receive message from server:".$data.PHP_EOL;
33 fwrite(STDOUT,"回复:");
34 $msg = trim(fgets(STDIN));
35 $cli->send($msg);
36 }
37
38 public function onError($cli){
39 echo "Connect faild".PHP_EOL;
40 }
41
42 public function onClose($cli){
43 echo "client connect close".PHP_EOL;
44 }
45
46 }
47
48 $client = new Client();
49 $client->connect();
5.启动Server.php,,Client.php
swoole1--搭建echo服务器的更多相关文章
- Linux下搭建SVN服务器及自动更新项目文件到web目录(www)的方法
首先搭建SVN服务器 1,安装SVN服务端 直接用apt-get或yum安装subversion即可(当然也可以自己去官方下载安装) sudo apt-get install subversion ...
- 【腾讯云】腾讯云服务器搭建ftp服务器
一.硬件描述 1.1 云服务器:腾讯云 云主机 操作系统:Ubuntu Server 14.04.1 LTS 32位 CPU:1核 内存:1GB 系统盘:20GB(本地磁盘) 数据盘:0GB 公网带宽 ...
- 原 Linux搭建SVN 服务器2
原 Linux搭建SVN 服务器 发表于1年前(2014-08-05 17:55) 阅读(12257) | 评论(3) 31人收藏此文章, 我要收藏 赞3 摘要 Linux搭建SVN 服务器 目录 ...
- 原 Linux搭建SVN 服务器
原 Linux搭建SVN 服务器 发表于1年前(2014-08-05 17:55) 阅读(12257) | 评论(3) 31人收藏此文章, 我要收藏 赞3 摘要 Linux搭建SVN 服务器 目录 ...
- 轻松使用Nginx搭建web服务器
如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,“一哥”( ...
- CenOS搭建FTP服务器
CenOS搭建FTP服务器 -------------------------------------------------------------------------准备工作--------- ...
- Linux搭建SVN 服务器
Linux搭建SVN 服务器 1 安装SVN 2 使用客户端连接 2.1 使用windows的客户端 2.2 使用Linux下的命令行 3 ...
- 应用SVN(CentOS中搭建SVN服务器)
简单介绍如何在虚拟机 CentOS 中,搭建 SVN 服务器. 软件版本信息 Vmware 10.0.0 build-1295980 CentOS 7.0-1406-x64 Java 1.7.0_67 ...
- Linux搭建SVN 服务器(转)
转自:http://my.oschina.net/lionel45/blog/298305 Linux搭建SVN 服务器 作者: 沈小然 日期:2014年 8月 5日 1 安装 ...
- 搭建git服务器及利用git hook自动布署代码
注意:服务器:Ubuntu Server 14.04,我的项目比较小,所有操作都使用root,建议最好新建一个用户(需要有管理员权限,否则在后面使用hooks自动部署代码时会出现各种权限问题,很蛋疼的 ...
随机推荐
- React路由的使用 Redirect默认展示某一个页面 Switch找到停止 BrowserRouter和HashRouter 的区别
引入 Redirect 默认展示某一个页面 Switch 一旦找到 路由 就停止 不会在往下找了 App.js import {Link,Route,NavLink,Redirect,Switch} ...
- Cookie与Session会话技术
Cookie与Session会话技术 一.什么是会话 会话:当用户打开浏览器,访问多个WEB资源,然后关闭浏览器的过程,称之为一个会话,选项卡,弹出页面都属于这个会话,且共享同一个session. 二 ...
- Serverless 实战——使用 Rendertron 搭建 Headless Chrome 渲染解决方案
为什么需要 Rendertron? 传统的 Web 页面,通常是服务端渲染的,而随着 SPA(Single-Page Application) 尤其是 React.Vue.Angular 为代表的前端 ...
- 在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
- MFC程序出现uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)解决办法
在同一个地方摔倒两次之后,决定记录下来这个东西. 问题 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl opera ...
- js Set对象
1.将数组转换成Set对象 let arr1 = new Set([1,2,3,4]) console.log(arr1) //{1,2,3,4} 2.数组去重 let arr2 = new Set( ...
- cesium 雷达扫描(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- 在服务器的tomcat中部署手机apk项目,浏览器或手机下载不能根据URL下载和安装apk文件
Android的APK包不能下载或安装,需在tomcat的web.xml加入 <mime-mapping> <extension>apk</extensio ...
- 【Cocos谁学谁会】制作会跑动的地板
版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...
- Netfilter,获取http明文用户名和密码
目录 Netfilter简介 实验-target端 内核模块的操作 初始化netfilter 解析http包,获取用户名和密码 实验-hack端 遇到的问题 @ Netfilter简介 Netfilt ...