TP5使用workerman开启websocket服务

前言

之前代码使用websocket方式是直接下载workman文件放入vendor中进行使用,这种方式处理及其容易出现问题,比如linux运行正常而windows无法使用,每次部署代码拉取依赖包都会覆盖等奇奇怪怪的问题

安装依赖

  1. 通过Composer.exe安装相关依赖:

    composer require topthink/think-worker

    如果报错,如下:

    composer require topthink/think-worker
    Using version ^2.0 for topthink/think-worker
    ./composer.json has been updated
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Your requirements could not be resolved to an installable set of packages.  
    Problem 1
    topthink/think-worker v2.0.5 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.4 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.3 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.2 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.1 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - topthink/think-worker v2.0.0 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability.    - Installation requestfortopthink/think-worker ^2.0 -> satisfiable by topthink/think-worker[v2.0.0, v2.0.1, v2.0.2, v2.0.3, v2.0.4, v2.0.5].Installation failed, reverting ./composer.json to its original content.
    Installation failed, reverting ./composer.json to its original content.

    则是版本不匹配,用低版本的workerman,更改如下:

    composer require topthink/think-worker=1.0.*

    如果是windows系统,还需安装依赖:

    composer require workerman/workerman-for-win

启动服务文件

在项目根目录里面新增server.php文件

<?php
define('APP_PATH', __DIR__ . '/../application/');
define("BIND_MODULE", "server/Screen");
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

增加控制器

<?php
namespace app\server\controller;

use think\worker\Server;

class Screen extends Server
{
    protected $socket = 'websocket://127.0.0.1:2346';
    private $all = [];

    /**
    * 收到信息
    * @param $connection
    * @param $data
    */
    public function onMessage($connection, $data)
    {
        // 广播
        foreach ($this->all as $v) {
            $v->send(json_encode("广播"));
        }
        // $connection->send("收到消息啦,您的id:" . $connection->id);
    }

    /**
    * 当连接建立时触发的回调函数
    * @param $connection
    */
    public function onConnect($connection)
    { 
        // 保存所有已连接的客户端id
        $this->all[$connection->id] = $connection;
    }
    /**
    * 当连接断开时触发的回调函数
    * @param $connection
    */
    public function onClose($connection)
    { 
        // 去除断开连接的客户端
        unset($this->all[$connection->id]);
    }

    /**
    * 当客户端的连接上发生错误时触发
    * @param $connection
    * @param $code
    * @param $msg
    */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg";
    } 

    /**
    * 每个进程启动
    * @param $worker
    */
    public function onWorkerStart($worker)
    {

    }
}

启动监听服务

打开cmd或bash执行以下代码

php server.php

测试

打开chrome浏览器, 先打开127.0.0.1域名下的网页( js跨域不能通讯),按F12打开调试控制台, 在console一栏输入(或者把下面代码放入到html页面用js运行)

ws = new WebSocket("ws://127.0.0.1:2346");
ws.onopen = function() {
    alert("连接成功");
    ws.send('tom');
    alert("给服务端发送一个字符串: tom");
};
ws.onmessage = function(e) {
    alert("收到服务端的消息: " + e.data);
};
ws.send('保持连接, 发第二次信息, 查看服务器回应');
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇