douzhangcuo2174 2017-08-23 02:50
浏览 111

线程内的Websocket

I'm trying to start a WebSocket server within threads, but it does not work fine. It runs with no error, the client get connected to the server, but it disconnects immediately. Does anyone know why? That's my code:

<?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();
}

The expected would be the client kept connected, and when the client send a message, receive back "Message received. Just any response back to see if it is working...", but it does not happen.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教