workerman事务,workerman event
使用Workerman事务和Event进行高效编程开发
在编程开发中,我们经常需要使用高效的工具和框架来提高开发效率和代码质量。其中,Workerman事务和Event就是两个非常优秀的工具,它们能够帮助我们实现高效的网络编程和事件处理。从编程开发者角度出发,介绍如何使用Workerman事务和Event进行高效编程开发。
什么是Workerman事务?
Workerman事务是一个基于PHP的高性能异步事件驱动框架,它能够实现高效的网络编程和事件处理。与传统的PHP框架不同,Workerman事务采用了多进程和事件驱动的方式,能够支持高并发和大量连接的处理。Workerman事务还提供了丰富的网络协议支持,包括TCP、UDP、http等,能够满足不同场景下的需求。
什么是Event?
Event是一个基于C语言的高性能事件处理库,它能够提供高效的事件驱动和异步IO操作。Event支持多种事件类型,包括网络事件、定时器事件、信号事件等,能够满足不同场景下的需求。Event还提供了多种IO模型,包括select、epoll、kqueue等,能够适应不同平台和系统。
如何使用Workerman事务和Event进行编程开发?
下面以一个简单的TCP 服务器 为例,介绍如何使用Workerman事务和Event进行编程开发。
我们需要安装Workerman事务和Event库。可以使用composer进行安装:
composer require workerman/workerman
composer require event
接着,我们可以编写一个简单的TCP服务器:
require_once __DIR__ . ‘/vendor/autoload.php’;
use WorkermanWorker;
use WorkermanConnectionTcpConnection;
// 创建一个Worker监听2345端口,使用Event事件库
$worker = new Worker(‘tcp://0.0.0.0:2345’);
$worker->transport = ‘tcp’;
$worker->onWorkerStart = function($worker) {
echo “Worker startedn”;
$worker->onConnect = function(TcpConnection $connection) {

echo “Client connectedn”;
$worker->onMessage = function(TcpConnection $connection, $data) {
echo “Received href="https://shuyeidc.com/wp/wp-content/uploads/2025/02/20250212061152-67ac3ba8b3276.jpg">
直接引语和间接引语专项练习
sister told me she was leaving for Lodon the next week. told me Mr. Smith had come here asked me if you wanted me to help you asked me if the man in the shop had understanded him at last asked me if I could speak English asked the boy how many there are people in your family mother said to the children dont worry said please give him a book said to me STOP writing and listening to me to me Which one do you like the best ? asked me Have you watched TV the night before asked me Are you busy today old worker said Dont forget the past. said I will take Mary here next time 1) 转述陈述句或感叹句去标点符号和引号,用say that接宾语从句,said后,从句中要作人称、时态等相应的变化。 asked me Who did have broken the window.
as 引导让步状语从句应注意什么?
as引导让步状语从句时,状语表语及谓语动词的一部分置于as之前,且表语 中的冠词要省略。 如:Try as we did we failed again.又如:Late as he came he saw the famous man.而although则无此种用法,句中although应改为 as. as引导让步状语从句时,常放在作表语,状语或谓语的一部分的形容词,名词,副词或动词原形之后.如果是单数名词或形容词的最高级作表语,不再用冠词. however和as 引导让步状语从句时的区别: however引导让步状语从句时,相当于no matter how,后面跟形容词或副词;as引导让步状语从句时,用倒装语序,即把作状语的副词或作表语的形容词或名词提前,而且如果作表语的是单数名词,前面不加冠词,也可以把谓语动词部分的实义动词提前。 〔误〕 As hard he works, he can’t catch up with his classmates. 〔正〕 However hard he works, he can’t catch up with his classmates. 〔析〕见上述说明。 〔误〕 A model worker he is, he remains modest. 〔正〕 Model worker as he is, he remains modest. 〔析〕as 引导让步状语从句,前面的单数名词前不加冠词。
如何使用WebSocket
引擎支持最新的WebSocket Version 13。 在C++中使用详细代码可参考引擎目录下的/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/文件。 头文件中的准备工作首先需要include WebSocket的头文件。 #include network/2d::network::WebSocket::Delegate定义了使用WebScocket需要监听的回调通知接口。 使用WebSocket的类,需要public继承这个Delegate。 class WebSocketTestLayer : public cocos2d::Layer, public cocos2d::network::WebSocket::Delegate并Override下面的4个接口:virtual void onOpen(cocos2d::network::WebSocket* ws);virtual void onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data);virtual void onClose(cocos2d::network::WebSocket* ws);virtual void onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error);后面我们再详细介绍每个回调接口的含义。 新建WebSocket并初始化 提供了一个专门用来测试WebSocket的服务器ws://。 测试代码以链接这个服务器为例,展示如何在Cocos2d-x中使用WebSocket。 新建一个WebSocket:cocos2d::network::WebSocket* _wsiSendText = new network::WebSocket();init第一个参数是delegate,设置为this,第二个参数是服务器地址。 URL中的ws://标识是WebSocket协议,加密的WebSocket为wss://._wsiSendText->init(*this, ws://)WebSocket消息监听在调用send发送消息之前,先来看下4个消息回调。 onOpeninit会触发WebSocket链接服务器,如果成功,WebSocket就会调用onOpen,告诉调用者,客户端到服务器的通讯链路已经成功建立,可以收发消息了。 void WebSocketTestLayer::onOpen(network::WebSocket* ws){if (ws == _wsiSendText){_sendTextStatus->setString(Send Text WS was opened.);}}onMessagenetwork::WebSocket::Data对象存储客户端接收到的数据, isBinary属性用来判断数据是二进制还是文本,len说明数据长度,bytes指向数据。 void WebSocketTestLayer::onMessage(network::WebSocket* ws, const network::WebSocket::Data& data){if (!){_sendTextTimes++;char times[100] = {0};sprintf(times, %d, _sendTextTimes);std::string textStr = std::string(response text msg: )++, +times;log(%s, textStr.c_str());_sendTextStatus->setString(textStr.c_str());}}onClose不管是服务器主动还是被动关闭了WebSocket,客户端将收到这个请求后,需要释放WebSocket内存,并养成良好的习惯:置空指针。 void WebSocketTestLayer::onClose(network::WebSocket* ws){if (ws == _wsiSendText){_wsiSendText = NULL;}CC_SAFE_DELETE(ws);}onError客户端发送的请求,如果发生错误,就会收到onError消息,游戏针对不同的错误码,做出相应的处理。 void WebSocketTestLayer::onError(network::WebSocket* ws, const network::WebSocket::ErrorCode& error){log(Error was fired, error code: %d, error);if (ws == _wsiSendText){char buf[100] = {0};sprintf(buf, an error was fired, code: %d, error);_sendTextStatus->setString(buf);}}send消息到服务器在init之后,我们就可以调用send接口,往服务器发送数据请求。 send有文本和二进制两中模式。 发送文本_wsiSendText->send(Hello WebSocket, Im a text message.);发送二进制数据(多了一个len参数)_wsiSendBinary->send((unsigned char*)buf, sizeof(buf));主动关闭WebSocket这是让整个流程变得完整的关键步骤, 当某个WebSocket的通讯不再使用的时候,我们必须手动关闭这个WebSocket与服务器的连接。 close会触发onClose消息,而后onClose里面,我们释放内存。 _wsiSendText->close();在Lua中使用详细代码可参考引擎目录下的/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/文件。 创建WebSocket对象脚本接口相对C++要简单很多,没有头文件,创建WebSocket对象使用下面的一行代码搞定。 参数是服务器地址。 wsSendText = WebSocket:create(ws://)定义并注册消息回调函数回调函数是普通的Lua function,4个消息回调和c++的用途一致,参考上面的说明。 local function wsSendTextOpen(strData)sendTextStatus:setString(Send Text WS was opened.)endlocal function wsSendTextMessage(strData)receiveTextTimes= receiveTextTimes + 1local strInfo= response text msg: .., :setString(strInfo)endlocal function wsSendTextClose(strData)print(_wsiSendText websocket instance closed.)sendTextStatus = nilwsSendText = nilendlocal function wsSendTextError(strData)print(sendText Error was fired)endLua的消息注册不同于C++的继承 & Override,有单独的接口registerScriptHandler。 registerScriptHandler第一个参数是回调函数名,第二个参数是回调类型。 每一个WebSocket实例都需要绑定一次。 if nil ~= wsSendText thenwsSendText:registerScriptHandler(wsSendTextOpen,_OPEN)wsSendText:registerScriptHandler(wsSendTextMessage,_MESSAGE)wsSendText:registerScriptHandler(wsSendTextClose,_CLOSE)wsSendText:registerScriptHandler(wsSendTextError,_ERROR)endsend消息Lua中发送不区分文本或二进制模式,均使用下面的接口。 wsSendText:sendString(Hello WebSocket中文, Im a text message.)主动关闭WebSocket当某个WebSocket的通讯不再使用的时候,我们必须手动关闭这个WebSocket与服务器的连接,以释放服务器和客户端的资源。 close会触发_CLOSE消息。 wsSendText:close()在JSB中使用详细代码可参考引擎目录下的/samples/Javascript/Shared/tests/ExtensionsTest/NetworkTest/文件。 创建WebSocket对象脚本接口相对C++要简单很多,没有头文件,创建WebSocket对象使用下面的一行代码搞定。 参数是服务器地址。 this._wsiSendText = new WebSocket(ws://);设置消息回调函数JSB中的回调函数是WebSocket实例的属性,使用匿名函数直接赋值给对应属性。 可以看出JS语言的特性,让绑定回调函数更加优美。 四个回调的含义,参考上面c++的描述。 this._ = function(evt) {self._(Send Text WS was opened.);};this._ = function(evt) {self._sendTextTimes++;var textStr = response text msg: ++, +self._sendTextTimes;(textStr);self._(textStr);};this._ = function(evt) {(sendText Error was fired);};this._ = function(evt) {(_wsiSendText websocket instance closed.);self._wsiSendText = null;};send消息发送文本,无需转换,代码如下:this._(Hello WebSocket中文, Im a text message.);发送二进制,测试代码中,使用_stringConvertToArray函数来转换string为二进制数据,模拟二进制的发送。 new Uint16Array创建一个16位无符号整数值的类型化数组,内容将初始化为0。 然后,循环读取字符串的每一个字符的Unicode编码,并存入Uint16Array,最终得到一个二进制对象。 _stringConvertToArray:function (strData) {if (!strData)returnnull;var arrData = new Uint16Array();for (var i = 0; i < ; i++) {arrData[i] = (i);}return arrData;},send二进制接口和send文本没有区别,区别在于传入的对象,JS内部自己知道对象是文本还是二进制数据,然后做不同的处理。 var buf = Hello WebSocket中文,\0 Im\0 a\0 binary\0 message\0.;var binary = this._stringConvertToArray(buf);this._();主动关闭WebSocket当某个WebSocket的通讯不再使用的时候,我们必须手动关闭这个WebSocket与服务器的连接,以释放服务器和客户端的资源。 close会触发onclose消息。 onExit: function() {if (this._wsiSendText)this._();}
发表评论