首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使棘轮WebSocket存活?

问如何使棘轮WebSocket存活?
EN

Stack Overflow用户
提问于 2020-04-28 20:25:28
回答 2查看 416关注 0票数 0

我有一个棘轮(PHP实时类)项目在我的本地主机.当我想使用和测试这个项目时,

我运行CMD并使用以下命令:C:\wamp\www\ChatApp> php cmd.php (cmd.php是websocket )。当我按Ctrl + C或退出cmd时,这个websocket就会死掉。

但是,当我将项目转移到主主机以激活这个websocket时,我能做些什么呢?

cmd.php:

代码语言:javascript
复制
require 'vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'Chat.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new \ChatApp\Chat()
        )
    ), 8080
);

$server->run();


$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ), 8080
);

$server->run();

Chat.php (类):

代码语言:javascript
复制
namespace ChatApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage();
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        echo 'Someone Connected'.PHP_EOL;
    }

    public function onMessage(ConnectionInterface $from,$msg) {
        foreach ($this->clients as $client) {
            if ($from !== $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn){
        $this->clients->detach($conn);
        echo 'Someone Has Disconnected'.PHP_EOL;
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An Error Has Occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-25 09:38:42

您可以在linux中使用( nohup )命令。在server.php根目录中编写这段代码

nohup server.php

票数 0
EN

Stack Overflow用户

发布于 2020-07-25 09:29:53

您可以在linux中使用( nohup )命令。在server.php根目录中编写这段代码

代码语言:javascript
复制
nohup php server.php &

若要了解有关此命令的更多信息,请参见浏览此链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61489377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档