问题描述
我将构建一个系统,该系统将接收流式原始数据表单浏览器。数据将使用websocket进行发送和接收,然后系统将继续将块数据发送到HTTP服务器。流程图如下所示:
首先,我将websocket服务器和HTTP客户端分成两个程序,两者都可以工作。 websocket服务器使用ratchet
,HTTP客户端使用stream_socket_client
和stream_select
。
然后,我尝试将websocket服务器和HTTP客户端结合在一起,我可以从浏览器接收数据,但无法从HTTP服务器获得任何响应。如何在Ratchet中获得HTTP响应?非常感谢。
Chat.PHP
<?PHP
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
function read_response_code($response){
foreach (explode("\n",$response) as $a) {
if (strpos($a,'HTTP/1.1') !== false) {
$arr = explode(" ",$a);
if ($arr[1] != "100")
{
return 0;
}
return 1;
}
}
return 0;
}
class Socket implements MessageComponentInterface {
protected $clients;
public function __construct(){
$this->clients = new \SplObjectStorage;
$this->SERVER = "tcp://192.168.0.1:80";
$this->socket = [];
$this->clients->connect = 0;
}
public function onopen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
echo "Try to connect http server...\n";
$this->socket = stream_socket_client($this->SERVER,$errno,$errstr,30);
if (!$this->socket){
//disconnect websocket
$conn->close();
}
else{
$header = "POST / HTTP/1.1\r\n"
. "Host: 192.168.0.1\r\n"
. "Accept: */*\r\n"
. "Content-Type: application/octet-stream\r\n"
. "transfer-encoding: chunked\r\n"
. "Expect: 100-continue\r\n\r\n";
$written = fwrite($this->socket,$header);
$response = fread($this->socket,100);
$ret = read_response_code($response);
if ($ret == 0)
{
echo "Connect HTTP server fail.\n";
$conn->close();
}
else
{
echo "Connect success!!!!\n";
$this->clients->connect = 1;
}
}
}
public function onMessage(ConnectionInterface $from,$msg) {
//combine http client
if ($this->clients->connect == 1)
{
$read = array($this->socket);
$write = NULL;
$except = NULL;
$num = stream_select($read,$write,$except,400000);
if ($num === false) {
/* Error handling */
}
elseif ($num > 0) {
// cannot read any response
echo "xxxxxxxxxxxxxx";
}
else {
// send data to HTTP server
$size = strlen($msg);
$hex = dechex($size);
fwrite($this->socket,"$hex\r\n");
$written = fwrite($this->socket,$msg);
fwrite($this->socket,"\r\n");
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed,remove from connection list
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn,\Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Chat-server.PHP
<?PHP
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.PHP';
$loop = \React\EventLoop\Factory::create();
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket()
)
),8080
);
$server->run();
HTTP_client.PHP
<?PHP
$SERVER = "tcp://192.168.0.1:80";
$SEND_STATUS = 0;
$file = fopen("test.raw","rb");
$socket = stream_socket_client($SERVER,30);
$header = "POST / HTTP/1.1\r\n"
. "Host: 192.168.0.1\r\n"
. "Accept: */*\r\n"
. "Content-Type: application/octet-stream\r\n"
. "transfer-encoding: chunked\r\n"
. "Expect: 100-continue\r\n\r\n";
$written = fwrite($socket,$header);
$response = fread($socket,100);
function send_data($file,$socket,$bytes) {
global $SEND_STATUS;
if ( feof($file) ) {
$SEND_STATUS = 1;
fwrite($socket,"0\r\n\r\n");
fclose($file);
return(false);
}
$buffer = fread($file,$bytes);
$size = strlen($buffer);
$hex = dechex($size);
fwrite($socket,"$hex\r\n");
$written = fwrite($socket,$buffer);
fwrite($socket,"\r\n");
}
while (true) {
$read = array($socket);
$write = NULL;
$except = NULL;
$num = stream_select($read,400000);
if ($num === false) {
/* Error handling */
}
elseif ($num > 0) {
foreach ($read as $r) {
echo "xxxxxxxxx";
}
}
else {
if ( $SEND_STATUS === 0 ) {
send_data($file,16000);
}
}
}
?>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)