TP5安装workerman版本并使用

通过Composer.exe安装相关依赖:
composer require topthink/think-worker

如果报错,如下:
composer require topthink/think-worker

Using version ^2.0 for topthink/think-worker

./composer.json has been updated

Loading composer repositories with package information

Updating dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.  

Problem 1    

- topthink/think-worker v2.0.5 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.4 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.3 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.2 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.1 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.0 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - Installation requestfortopthink/think-worker ^2.0 -> satisfiable by topthink/think-worker[v2.0.0, v2.0.1, v2.0.2, v2.0.3, v2.0.4, v2.0.5].Installation failed, reverting ./composer.json to its original content.

Installation failed, reverting ./composer.json to its original content.

则是版本不匹配,用低版本的workerman,更改如下:

composer require topthink/think-worker=1.0.*

如果是windows系统,还需安装依赖:
composer require topthink/think-worker=1.0.*

开始使用,按照手册说明进行操作,如下:

服务端使用示例如下:

新增启动服务文件server.php, 在项目根目录

#!/usr/bin/env php
<?php
define('APP_PATH', __DIR__ . '/application/');
define('BIND_MODULE','index/Worker');
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';

注:绑定模块中的index模块需要按照自已的需要修改,此处我改成了index模块,也就是默认的首模块。
新增服务处理类,位于index模块下,具体目录为application/index/controller
<?php
namespace app\index\controller;
use think\worker\Server;
class Worker extends Server
{
	protected $socket = 'websocket://127.0.0.1:2346';
	/**
	* 收到信息
	* @param $connection
	* @param $data
	*/
	public function onMessage($connection, $data)
	{
		$connection->send('我收到你的信息了');
	}
	
	/**
	* 当连接建立时触发的回调函数
	* @param $connection
	*/
	public function onConnect($connection)
	{ 
	
	}
	/**
	* 当连接断开时触发的回调函数
	* @param $connection
	*/
	public function onClose($connection)
	{ 
	
	}
	
	/**
	* 当客户端的连接上发生错误时触发
	* @param $connection
	* @param $code
	* @param $msg
	*/
	public function onError($connection, $code, $msg)
	{
		echo "error $code $msg\n";
	} 
	
	/**
	* 每个进程启动
	* @param $worker
	*/
	public function onWorkerStart($worker)
	{
	
	}
}

在命令行下运行, 启动监听服务

php server.php

打开chrome浏览器, 先打开127.0.0.1域名下的网页( js跨域不能通讯) , 按F12打开调试控制台, 在Console一栏输入(或者把下面代码放入到html页面用js运行)

ws = new WebSocket("ws://127.0.0.1:2346");
ws.onopen = function() {
	alert("连接成功");
	ws.send('tom');
	alert("给服务端发送一个字符串: tom");
};
ws.onmessage = function(e) {
	alert("收到服务端的消息: " + e.data);
};

继续测试

ws.send('保持连接, 发第二次信息, 查看服务器回应');
posted @ 2020-01-03 09:42  老耗子  阅读(766)  评论(0编辑  收藏  举报