search
HomePHP FrameworkWorkermanHow to use workerman

How to use workerman

Dec 05, 2019 am 10:13 AM
workerman

How to use workerman

Workerman is an open source high-performance asynchronous PHP socket instant messaging framework. Supports high concurrency and ultra-high stability, and is widely used in mobile apps, mobile communications, WeChat applets, mobile game servers, online games, PHP chat rooms, hardware communications, smart homes, Internet of Vehicles, Internet of Things and other fields. development. (Recommended learning: workerman tutorial)

Supports TCP long connections, supports Websocket, HTTP and other protocols, and supports custom protocols. It has many high-performance components such as asynchronous Mysql, asynchronous Redis, asynchronous Http, MQTT IoT client, asynchronous message queue, etc.

workerman itself is a PHP multi-process server framework with modules for PHP process management and socket communication, so it can run independently without relying on containers such as php-fpm, nginx or apache.

1. First download the official Workerman framework

2.As a server side

in workererman Create a new file in the framework root directory

use Workerman\Worker;
require_once __DIR__ . '/Workerman/Autoloader.php';
// 注意:使用的是websocket协议
$ws_worker = new Worker("websocket://0.0.0.0:2000"); //这里最好是0.0.0.0
// 启动4个进程对外提供服务
$ws_worker->count = 4;
$ws_worker->onWorkerStart = function($ws_worker){ 
};
$ws_worker->onConnect = function($ws_worker){ 
};
$ws_worker->onMessage = function($connection, $data)use($ws_worker){ //如果想用$ws_worker的话需要 use传进来
};
$worker->onClose = function($connection){ 
};
php yaofile.php start  debug模式 启动
php yaofile.php start -d (守护进程)启动

3. Front-end code

ws = new WebSocket("ws://47.90.81.150:2000"); //服务器的ip
ws.onopen = function() {
console.log("连接成功");
}
ws.onmessage = function(e) {
console.log("收到服务端的消息:" + e.data);
}
foreach($ws_worker->connections as $connection) {
$connection->send($cachearray);
}

4.As a client

use Workerman\Connection\AsyncTcpConnection; //要引用这个文件
$con = new AsyncTcpConnection('ws://api.huobi.pro:443/ws'); //远程服务器的连接
// 设置以ssl加密方式访问,使之成为wss 
$con->transport = 'ssl';
$con->onConnect = function($con) {
$con->send($data);
}
$con->onMessage = function($con, $data) {
};
$con->connect();

The above is the detailed content of How to use workerman. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor