drsc10888 2018-02-09 10:01
浏览 429
已采纳

PHP Workerman - 客户端websocket连接问题

I'am jsut trying to set up very simple connection between Workerman lib for PHP and javascript client. I can't get how to set the url for websocket properly on the javascript client.

Workerman lib

Actually I'am using Cloud9 for testing purpose, then I want to move this sample to shared hosting.

This is my folder structure:

enter image description here

This is my php sample 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
";
};

// 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
";
};

// Run worker
Worker::runAll();

This is my client sample 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>

Then I run my script with this command $ 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.

Then I'am running apache server and there is timeout error when I open my page: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

So, could somebody gave an advice how to set websocket address on the client in this case to use it with Workerman lib properly, please?

  • 写回答

1条回答 默认 最新

  • duanaoreng9355 2018-02-09 10:07
    关注

    My first assumption is that this port is not opened by Cloud9.

    Reading their documentation kind of confirms that: 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services