workerman错误提示

zhaoxi123

我用thinkphp3.2自动加载的方式整合workerman,加载成功,但是用微信小程序开启websocket连接时workerman端就会自动关闭连接,错误提示为error package. package_length=false,如附件所示,请高手赐教
!

7404 3 0
3个回答

walkor 打赏

用错协议了,应该是
websocket://0.0.0.0:4433
监听最好用0.0.0.0,只监听127.0.0.1只能本机连,其它服务器或者外网是连不上你的websocket服务的。
参考手册:http://doc.workerman.net/315128

ws是workerman用做客户端去连别的websocket服务端用的协议,参考手册 http://doc.workerman.net/315306

  • zhaoxi123 2017-09-21

    恩,我想用ws://0.0.0.0:4433,因为想用在微信小程序的调试上,按照您的建议并结合手册改了下,会有error: connect 0.0.0.0:4433 fail after 0.0016 seconds的提示,请帮忙看下

zhaoxi123
<?php
namespace Home\Controller;
use Think\Controller;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Connection\AsyncTcpConnection;  
use Workerman\Lib\Timer;  
require_once 'C:/wamp/www/lei/ThinkPHP/Library/Workerman/Autoloader.class.php';
class WorkerManController{
    public function index(){
        if(!IS_CLI){
            die("无法直接访问,请通过命令行启动");
        }
        TcpConnection::$maxPackageSize = 1024000;
        Worker::$daemonize=true;
        $worker = new \Workerman\Worker();
        $worker->name='HHN';
        $worker->count=1;
        // 当收到客户端发来的数据后返回hello $data给客户端
        $worker->onWorkerStart = function($worker){
            echo "Worker starting...\n";
             // 以websocket协议连接远程websocket服务器
    $ws_connection = new AsyncTcpConnection('ws://0.0.0.0:4433');
    // 连上后发送hello字符串
    $ws_connection->onConnect = function($connection){
        $connection->send('hello');
    };
    // 远程websocket服务器发来消息时
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    // 连接上发生错误时,一般是连接远程websocket服务器失败错误
    $ws_connection->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
    };
    // 当连接远程websocket服务器的连接断开时
    $ws_connection->onClose = function($connection){
        echo "connection closed\n";
    };
    // 设置好以上各种回调后,执行连接操作
    $ws_connection->connect();
        };
        $worker->onMessage = function($connection, $data)
        {
            // 向客户端发送hello $data
            $connection->send('hello '.$data);
        };
        $worker->onClose = function($connection)
        {
            echo "connection 关闭\n";
        };
        // 运行worker
        Worker::runAll();

    }
}
  • 暂无评论
walkor 打赏

监听是监听0.0.0.0,意思是监听本机所有网卡ip。

但是作为客户端连别的服务器时不能用0.0.0.0,要用实际的ip,本机的话就是127.0.0.1

年代过于久远,无法发表回答
×
🔝