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:

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

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

// 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

This is my client sample index.html:

<!DOCTYPE html>
<html lang="en">
    <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>
    <script type="text/javascript">
        // How to set url for websocket in this case???
        var socket = new WebSocket("wss://");

        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 " +;

        socket.onerror = function(error) {
          alert("Error" + error.message);

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://   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?

2018/02/09 10:01
  • websocket
  • php
  • javascript
  • 点赞
  • 收藏
  • 回答