首页 > 代码库 > swoole websocket和TCPsocket的交互
swoole websocket和TCPsocket的交互
<?php | |
/** | |
* Created by PhpStorm. | |
* User: lancelot | |
* Date: 16-7-30 | |
* Time: 下午8:13 | |
*/ | |
class Server | |
{ | |
private $serv; | |
/** | |
* @var PDO | |
*/ | |
private $pdo; | |
public function __construct() | |
{ | |
$this->serv = new swoole_websocket_server("0.0.0.0", 9501); | |
$this->serv->set([ | |
‘worker_num‘ => 1, | |
‘dispatch_mode‘ => 2, | |
‘daemonize‘ => 0, | |
]); | |
$this->serv->on(‘message‘, array($this, ‘onMessage‘)); | |
$this->serv->on(‘Request‘, array($this, ‘onRequest‘)); | |
$port1 = $this->serv->listen("0.0.0.0", 9503, SWOOLE_SOCK_TCP); | |
$port1->set( | |
[ | |
‘open_eof_split‘=> true,//检测自动分包打开(具体干什么的 我不懂) | |
‘package_eof‘ => "\r\n" | |
] | |
); | |
$port1->on(‘Receive‘, array($this, ‘onTcpReceive‘)); | |
$this->serv->start(); | |
} | |
//显示是哪个客户端发来的数据 | |
public function onMessage(swoole_websocket_server $_server, $frame) | |
{ | |
foreach($_server->connections as $fd) | |
{ | |
$info = $_server->connection_info($fd); | |
var_dump($info); | |
} | |
} | |
//服务端接收到不同端口的数据如何处理 | |
public function onRequest($request, $response) | |
{ | |
foreach($this->serv->connections as $fd) | |
{ | |
$info = $this->serv->connection_info($fd); | |
switch($info[‘server_port‘]) | |
{ | |
case 9501: | |
{ | |
// websocket | |
if($info[‘websocket_status‘]) | |
{ | |
} | |
$response->end(""); | |
} | |
case 9503: | |
{ | |
// TCP | |
} | |
} | |
var_dump($info); | |
} | |
} | |
public function onTcpReceive( swoole_server $serv, $fd, $from_id, $data ) { | |
var_dump($data); | |
$data_list = explode("\r\n", $data); | |
foreach ($data_list as $msg) { | |
if( !empty($msg) ) { | |
echo "Get Message From Client {$fd}:{$msg}\n"; | |
} | |
} | |
} | |
} | |
new Server(); |
swoole websocket和TCPsocket的交互
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。