相关疑难解决方法(0)

如何在PHP中创建websockets服务器

是否有任何教程或指南显示如何在PHP中编写一个简单的websockets服务器?我试过在谷歌上寻找它,但我找不到很多.我发现phpwebsockets但它现在已经过时,不支持最新的协议.我自己尝试更新它,但它似乎不起作用.

#!/php -q
<?php  /*  >php -q server.php  */

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$master  = WebSocket("localhost",12345);
$sockets = array($master);
$users   = array();
$debug   = false;

while(true){
  $changed = $sockets;
  socket_select($changed,$write=NULL,$except=NULL,NULL);
  foreach($changed as $socket){
    if($socket==$master){
      $client=socket_accept($master);
      if($client<0){ console("socket_accept() failed"); continue; }
      else{ connect($client); }
    }
    else{
      $bytes = @socket_recv($socket,$buffer,2048,0);
      if($bytes==0){ disconnect($socket); }
      else{
        $user = getuserbysocket($socket);
        if(!$user->handshake){ dohandshake($user,$buffer); }
        else{ process($user,$buffer); }
      }
    }
  }
}

//---------------------------------------------------------------
function process($user,$msg){
  $action = unwrap($msg);
  say("< ".$action);
  switch($action){
    case "hello" : send($user->socket,"hello human");                       break;
    case …
Run Code Online (Sandbox Code Playgroud)

javascript php websocket

75
推荐指数
4
解决办法
15万
查看次数

WebSockets - 通过 php 发送 json 数据

我正在尝试从 PHP 向我的 websocket aws api 网关发送一个即发即弃的请求。

我已经设置了一个名为“发送消息”的操作。

这是我正在使用的代码:

$protocol = "ssl";
$host = "<myendpoint>.amazonaws.com";
$port = 443;
$path = "/<mystage>/";
$timeout = 2000;

$socket = pfsockopen($protocol . "://" . $host, $port,
                    $errno, $errstr, $timeout);

$content = "{'action': 'sendmessage', 'data': 'test'}";
$body = "POST $path HTTP/1.1\r\n";
$body .= "Host: $host\r\n";
$body .= "Content-Type: application/json\r\n";
$body .= "Content-Length: " . strlen($content) . "\r\n";
$body .= "Connection: Close\r\n\r\n";
$body .= $content;
$body .= "\r\n";

fwrite($socket, $body);
Run Code Online (Sandbox Code Playgroud)

然而,什么也没有发生。

如果我使用 wscat,例如:

wscat -c wss://<my-endpoint>.amazonaws.com/<my-stage> …
Run Code Online (Sandbox Code Playgroud)

php amazon-web-services websocket aws-api-gateway

7
推荐指数
1
解决办法
2635
查看次数

网页如何向本地网络发送消息

我们的Web应用程序有一个按钮,可以将数据发送到本地网络上的服务器,然后在打印机上打印.

到目前为止很容易:按钮触发了一个http://printerserver/print.php带有令牌的AJAX POST请求,该页面连接到Web应用程序以验证令牌并获取要打印的数据然后打印.

但是,我们现在通过HTTP提供我们的Web应用程序(我宁愿不再回到HTTP),而新版本的Chrome和Firefox不再向HTTP地址发出请求,他们甚至不发送请求检查CORS标头.

现在,什么是跨协议XHR的现代替代品?Websockets是否遭遇同样的问题?(谷歌搜索没有说明这里的当前状态是什么.)我可以使用TCP套接字吗?我也不愿意切换到GET请求,因为该动作不是幂等的,它可能对预加载和缓存有实际意义.

我可以以任何方式更改打印机服务器上的应用程序(因此我可以用NodeJS或其他东西替换它)但我无法更改用户的浏览器(例如,信任打印机服务器的自签名证书).

https websocket cors mixed-content

6
推荐指数
1
解决办法
639
查看次数

使用PHP连接到棘轮Websocket服务器

我在后端运行Ratchet WebSocketServer,一切正常。

<?php

require '../vendor/autoload.php';

use Ratchet\WebSocket\WsServer;
use Ratchet\Http\HttpServer;

$wsServer = new WsServer(new Chat());
$wsServer->disableVersion(0);

$server = \Ratchet\Server\IoServer::factory(
    new HttpServer(
        $wsServer
    ),
    8080
);

$server->run();
Run Code Online (Sandbox Code Playgroud)

但是我想使用简单的php脚本连接到websocket,以将消息发送到服务器。

$host = 'ws://localhost';  //where is the websocket server
$port = 8080;
$local = "http://example.com";  //url where this script run 
$data = "first message";  //data to be send

$head = "GET / HTTP/1.1"."\r\n".
    "Upgrade: WebSocket"."\r\n".
    "Connection: Upgrade"."\r\n".
    "Origin: $local"."\r\n".
    "Host: $host:$port"."\r\n".
    "Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
    "Content-Length: ".strlen($data)."\r\n"."\r\n";

//WebSocket handshake
$sock = fsockopen($host, $port);
fwrite($sock, $head ) or …
Run Code Online (Sandbox Code Playgroud)

php sockets php-socket websocket ratchet

3
推荐指数
1
解决办法
5057
查看次数