zoukankan      html  css  js  c++  java
  • WorkerMan 入门学习之(二)基础教程-Connection类的使用

    一、TcpConnection类 的使用

     1、简单的TCP测试

    Server.php

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

    测试结果:

    服务端输出:

    以下错误表示,里面的连接协议没有修改掉原因。

    www@iZ23s8agtagZ:~$ telnet 127.0.0.1 8085
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    ^[[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、黑白名单的访问

    Server.php

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

    开启Workerman服务

    正确的访问:

    非本地地址访问:

     

     二、AsyncTcpConnection类 的使用

    Server.php

    <?php
    require_once __DIR__.'/Workerman/Autoloader.php';
    use WorkermanWorker;
    $worker = new Worker('websocket://0.0.0.0:443');
    // Workerman 启动的回调,这里传递的是Worker对象
    $worker->onWorkerStart = function ($worker){
        echo "onWorkerStart success";
    };
    // 连接回调
    $worker->onConnect = function ($connection){
        $connection_baidu = new WorkermanConnectionAsyncTcpConnection('tcp://www.baidu.com:443');
        // 百度的数据发送给浏览器。返回数据后,使用的数据要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 
    ";
    };
    
    //Workerman 停止回调
    $worker->onWorkerStop = function ($worker){
        echo "onWorkerStop success";
    };
    $worker::runAll();

     

  • 相关阅读:
    【转】Visual Studio 2008中使用科学计算库GSL
    【转】[Python Tip]如何在Windows下方便地进入命令行运行程序
    【转】URL Encoding (URL转义字符)
    RDLC报表,纯文字内容,动态数据源 解决方案
    C# 数值计算
    标准库List使用注意
    VS2008 运行出现 “无法启动该程序 计算机中丢失 MSVCR90D.dll”
    数据结构在游戏中的简单应用(转)
    SQL SERVER 2005 四种排序函数
    2010.11.30
  • 原文地址:https://www.cnblogs.com/tinywan/p/7414251.html
Copyright © 2011-2022 走看看