简体   繁体   中英

PHP Workerman - client websocket connection issue

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:

在此处输入图片说明

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\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();

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM