|   注册

安装

# Archlinux
yaourt -S php-swoole
# 加载扩展
vi /etc/php/conf.d/swoole.ini

一、服务器端( server.php )

$server = new swoole_websocket_server("127.0.0.1", 9502);

$server->on('open', function($server, $req) {
    echo "connection open: {$req->fd}\n";
});

$server->on('message', function($server, $frame) {
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(["hello", "world"]));
});

$server->on('close', function($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();

启动服务

php -S localhost:8001 server.php

二、客户端端( client.html )

<script type="text/javascript">
    var socket = new WebSocket('ws://127.0.0.1:9502/');
    // 链接服务器
    socket.onopen = function(event) {
        console.log('链接成功!');
    }
    // 接收消息
    socket.onmessage = function(event) {
        console.log(event);
    }
    // 发送消息
    // socket.send('客户端消息!!');
</script>

WebMIS

基于MVC框架开发的多用户、多权限开源解决方案,整合了Jquery、TinyMCE、ChartJS等插件!

视频教程
Linux教程
PHP教程
JavaScript教程
MVC框架
自建HMVC框架
Phalcon框架
Lumen框架
Linux文档
Shell文档
CentOS文档
Archlinux文档
WebMIS © 2017-2020 备案号: 滇ICP备14000710号
https://webmis.vip/home/