How to call swoole asynchronously?
Test case:
Rewrite ws_server. php
$this->ws->set( [ 'worker_num' => 2, 'task_worker_num' => 2, ] ); $this->ws->on("open", [$this, 'onOpen']); $this->ws->on("message", [$this, 'onMessage']); $this->ws->on("task", [$this, 'onTask']); $this->ws->on("finish", [$this, 'onFinish']); $this->ws->on("close", [$this, 'onClose']); public function onOpen($ws, $request) { var_dump($request->fd); } public function onMessage($ws, $frame) { echo "ser-push-message:{$frame->data}\n"; $data = [ 'task' => 1, 'fd' => $frame->fd, ]; $ws->task($data); $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s")); } public function onTask($serv, $taskId, $workerId, $data) { print_r($data); // 耗时场景 10s sleep(10); return "on task finish"; // 告诉worker } public function onFinish($serv, $taskId, $data) { echo "taskId:{$taskId}\n"; echo "finish-data-sucess:{$data}\n"; }
The client, i.e. ws_client.html, sends information. The server's onMessage accepts and executes the setting data to call onTask. It prints the data in onTask and returns the data to onFinish after 3 seconds. OnFinish accepts the data and outputs it.
User page:
<!DOCTYPE html> <head> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <h1 id="chuangxiang-测试">chuangxiang--测试</h1> <script> var wsUrl = "ws://192.168.0.103:8812"; var websocket = new WebSocket(wsUrl); //实例对象的onopen属性 websocket.onopen = function(evt) { websocket.send("Hello chaungxiang");//输出到服务器端,即发送信息 console.log("connected-swoole-success");//输出在页面 } //实例化 onmessage websocket.onmessage = function(evt) { console.log("ws-server-return-data:"+evt.data); } //实例化onclose websocket.onclose = function(evt) { console.log("close"); } //实例化onerror websocket.onerror =function(evt,e) { console.log("error:"+evt.data); } </script> </body> </html>
Browser: (Client)
Server:
The first line of data is output by the server. At the beginning, the connection is established in onOpen(), and then the client (ws_client.html) connects and connects through js code. Send data; the second line prints before sending data; after sending data to the server, execute onMessage(); output the data again and create a task, call onTask(); output the data again and return the data to onFinish();
The server outputs directly to the server, and the client needs to send it to the server through push, send, etc.
The client prints data to the page through console.log
Asynchronous manifestation
public function onMessage($ws, $frame) { echo "ser-push-message:{$frame->data}\n"; $data = [ 'task' => 1, 'fd' => $frame->fd, ]; $ws->task($data); $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s")); }
After a task is created in this code, it will not wait until the task is executed before pushing, but will proceed at the same time. After the client prints and sends the data, the interval between the data printed by the client and the output in onMessage is very short, and will not be affected by the sleep in the ontask task.
The above is the detailed content of How to call swoole asynchronously. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
