问题描述
我在 laravel 上使用 websocket 的 cboden/ratchet 包。这是地球上最令人沮丧的事情。
我什至如何将其连接起来?
// 这是我启动服务器的测试命令
<?PHP
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Classes\Socket\ChatSocket as Socket;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ratched:serve';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Just For Testing';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket()
)
),8090
);
$server->run();
}
}
那么这是我的客户端
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<Meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2>Testing RATCHET</h2>
<div class="container">
<div class="content">
<div class="title">Laravel</div>
<button onclick="send()">Submit</button>
</div>
</div>
<script>
var ws = new WebSocket("ws://localhost:8090/");
ws.onopen = function () {
// Websocket is connected
console.log("Websocket connected");
ws.send("Hello World");
console.log("Message sent");
};
ws.onmessage = function (event) {
// Message received
console.log("Message received = " + event.data);
};
ws.onclose = function () {
// websocket is closed.
console.log("Connection closed");
};
</script>
</body>
</html>
//连接逻辑
<?PHP
namespace App\Classes\Socket;
// use App\Classes\Socket\Base\BaseSocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatSocket implements MessageComponentInterface
{
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onopen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from,$msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n",$from->resourceId,$msg,$numRecv,$numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver,send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed,remove it,as we can no longer send it messages
$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();
}
}
我尝试过更改端口,没有运气
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)