gpt4 book ai didi

javascript - PHP Workerman - 客户端 websocket 连接问题

转载 作者:行者123 更新时间:2023-12-03 02:31:52 27 4
gpt4 key购买 nike

我刚刚尝试在 PHP 的 Workerman 库和 javascript 客户端之间建立非常简单的连接。我不知道如何在 javascript 客户端上正确设置 websocket 的 url。

Workerman lib

实际上我使用 Cloud9 进行测试,然后我想将此示例移至共享托管。

这是我的文件夹结构:

enter image description here

这是我的 php 示例 start.php:

<?php
require_once '/home/ubuntu/workspace/workerman/vendor/autoload.php';
use Workerman\Worker;

// Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:2346");

// 4 processes
$ws_worker->count = 4;

// Emitted when new connection come
$ws_worker->onConnect = function($connection)
{
echo "New connection\n";
};

// Emitted when data received
$ws_worker->onMessage = function($connection, $data)
{
// Send hello $data
$connection->send('hello ' . $data);
};

// Emitted when connection closed
$ws_worker->onClose = function($connection)
{
echo "Connection closed\n";
};

// Run worker
Worker::runAll();

这是我的客户端示例index.html:

<!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>Workerman Sockets Test</title>
</head>
<body>
<h3>Hello</h3>
<script type="text/javascript">
// How to set url for websocket in this case???
var socket = new WebSocket("wss://project-user.c9users.io:2346/workerman/test/");

socket.onopen = function() {
alert("Connection established.");
};

socket.onclose = function(event) {
if (event.wasClean) {
alert('The connection is closed.');
} else {
alert('Connection failure'); // for example, the server process is "killed"
}
alert('Code: ' + event.code + ' reason: ' + event.reason);
};

socket.onmessage = function(event) {
alert("Received data " + event.data);
};

socket.onerror = function(error) {
alert("Error" + error.message);
};
</script>
</body>
</html>

然后我使用此命令运行脚本 $ php start.php:

Workerman[start.php] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.4 PHP version:5.5.9-1ubuntu4.22
------------------------ WORKERS -------------------------------
user worker listen processes status
ubuntu none websocket://0.0.0.0:2346 4 [OK]
----------------------------------------------------------------
Press Ctrl+C to quit. Start success.

然后我正在运行 apache 服务器,当我打开页面时出现超时错误:连接建立时出错:net::ERR_CONNECTION_TIMED_OUT

那么,有人可以建议如何在这种情况下在客户端上设置 websocket 地址,以便正确地将其与 Workerman lib 一起使用吗?

最佳答案

我的第一个假设是Cloud9没有打开这个端口。

阅读他们的文档可以确认: https://docs.c9.io/docs/run-an-application

Available ports on a hosted Cloud9 workspace If you're developing a server application, please note that you need to listen to 0.0.0.0 ($IP) and 8080 ($PORT). Listening to this port will enable your app to be viewable at http://-.c9users.io

You can also bind to ports 8081, and 8082, which can be accessed by http://-.c9users.io:8081 and http://-.c9users.io:8082 respectively.

Please note that 8080, 8081, and 8082 are the only available ports on a hosted Cloud9 workspace.

关于javascript - PHP Workerman - 客户端 websocket 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703224/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com