线程内的Websocket

时间:2017-08-23 02:50:16

标签: php multithreading websocket pthreads

我正在尝试在线程中启动WebSocket服务器,但它不能正常工作。它运行时没有错误,客户端连接到服务器,但它立即断开连接。有谁知道为什么?那是我的代码:

<?php

require 'websocket.class.php';

class AsyncOperation extends Thread {

    public function __construct($arg) {
        $this->arg = $arg;
    }

    public function wsOnMessage($clientID, $message, $messageLength, $binary) {
        global $Server;
        $Server->wsSend($clientID, "Message received. Just any response back to see if it is working...");
    }

    public function wsOnOpen($clientID)
    {
        global $Server;
        $Server->wsSend($clientID, "client $clientID is connected");
    }

    public function run() {
        $Server = new PHPWebSocket();
        $Server->bind('message', 'wsOnMessage');
        $Server->bind('open', 'wsOnOpen');
        $Server->bind('close', 'wsOnClose');
        $Server->wsStartServer('localhost', 9300);
    }

}

//These data are gonna be used later - not important now
$charts = array(
    array("name" => "Blablabla")
);

$stack = array();

// Initiate Multiple Thread
foreach ($charts as $c) {
    $stack[] = new AsyncOperation($c);
}

// Start The Threads
foreach ($stack as $t) {
    $t->start();
}

预期的是客户端保持连接,当客户端发送消息时,收到“收到消息。回复任何响应以查看它是否正常......”,但它不会发生。

0 个答案:

没有答案
相关问题