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