Home  >  Article  >  PHP Framework  >  Use of WorkerMan Connection class (with code)

Use of WorkerMan Connection class (with code)

尚
forward
2019-11-26 15:18:253146browse

The following column workerman Tutorial will introduce you to the use of the WorkerMan Connection class. I hope it will be helpful to friends in need!

Use of WorkerMan Connection class (with code)

1. Use of TcpConnection class

1. Simple TCP test

Server.php

<?php
require_once __DIR__.&#39;/Workerman/Autoloader.php&#39;;
use Workerman\Worker;
$worker = new Worker(&#39;websocket://0.0.0.0:80&#39;);// 连接回调
$worker->onConnect = function ($connection){
     echo "connection success\n";
};
// 接受发送消息
$worker->onMessage = function ($conn,$data){
    $conn->send("Hello World");
};
// 关闭连接
$worker->onClose = function ($connection){
    echo "connection close \n";
};
$worker::runAll();

Test results:

Use of WorkerMan Connection class (with code)

Server output:

Use of WorkerMan Connection class (with code)

The following errors indicate that the connection protocol inside The reason has not been modified.

www@iZ23s8agtagZ:~$ telnet 127.0.0.1 8085
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is &#39;^]&#39;.
^[[A^[[A
HTTP/1.1 400 Bad Request

<b>400 Bad Request</b><br>Invalid handshake data for websocket. <br> See <a href="http://wiki.workerman.net/Error1">http://wiki.workerman.net/Error1</a> for detail.Connection closed by foreign host.

2. Access to black and white lists

Server.php

<?php
require_once __DIR__.&#39;/Workerman/Autoloader.php&#39;;
use Workerman\Worker;
$worker = new Worker(&#39;tcp://0.0.0.0:8085&#39;);
// 连接回调
$worker->onConnect = function ($connection){
    // IP 白名单验证
    if($connection->getRemoteIP() != &#39;127.0.0.1&#39;){
        $connection->close("IP Address Forbidden");
    }
};
// 接受发送消息
$worker->onMessage = function ($conn,$data){
    $conn->send("Hello World");
};
// 关闭连接
$worker->onClose = function ($connection){
    echo "connection close \n";
};
$worker::runAll();

Open Workerman service

Use of WorkerMan Connection class (with code)

Correct access :

Use of WorkerMan Connection class (with code)

Use of WorkerMan Connection class (with code)

##Non-local address access:

Use of WorkerMan Connection class (with code)

2. The use of AsyncTcpConnection class

Server.php

<?php
require_once __DIR__.&#39;/Workerman/Autoloader.php&#39;;
use Workerman\Worker;
$worker = new Worker(&#39;websocket://0.0.0.0:443&#39;);
// Workerman 启动的回调,这里传递的是Worker对象
$worker->onWorkerStart = function ($worker){
    echo "onWorkerStart success";
};
// 连接回调
$worker->onConnect = function ($connection){
    $connection_baidu = new \Workerman\Connection\AsyncTcpConnection(&#39;tcp://www.baidu.com:443&#39;);
    // 百度的数据发送给浏览器。返回数据后,使用的数据要use 进来,
    $connection_baidu->onMessage = function ($connection_baidu,$data) use ($connection){
        $connection->send($data);
    };
    // 浏览器接受的数据发送给百度
    $connection->onMessage = function ($connection,$data) use ($connection_baidu){
        $connection_baidu->send($data);
    };
    $connection_baidu->connect();
};
// 接受发送消息
$worker->onMessage = function ($conn,$data){
    $conn->send("Hello World");
};
// 关闭连接
$worker->onClose = function ($connection){
    echo "connection close \n";
};

//Workerman 停止回调
$worker->onWorkerStop = function ($worker){
    echo "onWorkerStop success";
};
$worker::runAll();

For more WorkerMan related articles, please pay attention to the

WorkerMan usage tutorial column.

The above is the detailed content of Use of WorkerMan Connection class (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What does worker mean?Next article:What does worker mean?