Home > PHP Framework > Workerman > body text

How to use workerman

(*-*)浩
Release: 2019-12-05 10:13:05
Original
2751 people have browsed it

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 (守护进程)启动
Copy after login

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);
}
Copy after login

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();
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!