用workerman做个端口转发,贴点代码,就是根据各个端口数据的不同特征导向各自的端口
// tcp连接建立后 $worker->onConnect = function(TcpConnection $connection) { // 设置连接的onMessage回调 $connection->onMessage = function(TcpConnection $connection, $data) { if (strpos($data,’Sec-WebSocket-Version: 13′) !== false) { //echo "websocket rn"; // 建立本地websocket端口的异步连接 $connection_to_ws = new AsyncTcpConnection(‘tcp://127.0.0.1:8787’); // 设置将当前客户端连接的数据导向websocket端口的连接 $connection->pipe($connection_to_ws); // 设置webscoket端口连接返回的数据导向客户端连接 $connection_to_ws->pipe($connection); // 执行异步连接 $connection_to_ws->connect(); $connection_to_ws->send($data); } else if (strpos($data,’HTTP/1.1′) !== false) { //echo "http rn"; // 建立本地http端口的异步连接 $connection_to_http = new AsyncTcpConnection(‘tcp://127.0.0.1:8080’); // 设置将当前客户端连接的数据导向http端口的连接 $connection->pipe($connection_to_http); // 设置http端口连接返回的数据导向客户端连接 $connection_to_http->pipe($connection); // 执行异步连接 $connection_to_http->connect(); $connection_to_http->send($data); } else if (strpos($data,’redis’) !== false) { //在密码中一定加入redis关键字便于判断 //echo "redis rn"; // 建立本地redis端口的异步连接 $connection_to_redis = new AsyncTcpConnection(‘tcp://127.0.0.1:6379’); // 设置将当前客户端连接的数据导向redis端口的连接 $connection->pipe($connection_to_redis); // 设置redis端口连接返回的数据导向客户端连接 $connection_to_redis->pipe($connection); // 执行异步连接 $connection_to_redis->connect(); $connection_to_redis->send($data); } else { //echo "ssh rn"; // 建立本地ssh端口的异步连接 $connection_to_ssh = new AsyncTcpConnection(‘tcp://127.0.0.1:22’); // 设置将当前客户端连接的数据导向ssh端口的连接 $connection->pipe($connection_to_ssh); // 设置ssh端口连接返回的数据导向客户端连接 $connection_to_ssh->pipe($connection); // 执行异步连接 $connection_to_ssh->connect(); $connection_to_ssh->send($data); } |