Connect to websocket on real server, domain or ip server?
ananchevegor opened this issue · comments
Hey, i have this problem. I have website on real server, i have domain and ip adress.
i have this code on server side and when i start it, the server write start success
php server.php start
Workerman[server.php] start in DEBUG mode
-------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.1.11 PHP version:7.2.24 Event-Loop:\Workerman\Events\Select
--------------------------------------------- WORKERS ---------------------------------------------
proto user worker listen processes status
ssl user none websocket://0.0.0.0:2346 1 [OK]
---------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
this is code from server.php
<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// SSL context.
$context = [
'ssl' => [
'local_cert' => 'config/keys/cert.pem',
'local_pk' => 'config/keys/key.pem',
'verify_peer' => false,
]
];
// Create a Websocket server with ssl context.
$ws_worker = new Worker('websocket://0.0.0.0:2346', $context);
// Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://).
// The similar approaches for Https etc.
$ws_worker->transport = 'ssl';
$ws_worker->onMessage = function ($connection, $data) {
// Send hello $data
$connection->send('Hello ' . $data);
};
Worker::runAll();
?>
i should use wss because site has https protocol.
and when i write scripts in html i used
let ws = new WebSocket("wss://ip_real_server:2346");
and become this error
Event {isTrusted: true, type: 'error', target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}
isTrusted
:
true
bubbles
:
false
cancelBubble
:
false
cancelable
:
false
composed
:
false
currentTarget
:
WebSocket {url: 'wss://ip_real_server:2346/', readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
defaultPrevented
:
false
eventPhase
:
0
returnValue
:
true
srcElement
:
WebSocket {url: 'wss://ip_real_server:2346/', readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
target
:
WebSocket {url: 'wss://ip_real_server:2346/', readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
timeStamp
:
21290.5
type
:
"error"
[[Prototype]]
:
Event
everywhere when wrote ip_real_server its ip my real server.
so, and question, what i need to use instead of let ws = new WebSocket("wss://ip_real_server:2346");
should i use website's domain? or something else?
Please try let ws = new WebSocket("wss://yourdomain:2346");
Please try
let ws = new WebSocket("wss://yourdomain:2346");
i tried it
anyway i have this error even when wtire domain
ws.php:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$context = [
'ssl' => [
'local_cert' => __DIR__ .'/ssl/server.pem',
'local_pk' => __DIR__ .'/ssl/server.key',
'verify_peer' => false,
]
];
$worker = new Worker('websocket://0.0.0.0:8282', $context);
$worker->transport = 'ssl';
$worker->onMessage = function(TcpConnection $con, $msg) {
$con->send('ok');
};
Worker::runAll();
js code:
ws = new WebSocket("wss://mydomain.com:8282");
ws.onopen = function() {
alert("连接成功");
ws.send('tom');
alert("给服务端发送一个字符串:tom");
};
ws.onmessage = function(e) {
alert("收到服务端的消息:" + e.data);
};
Thanks all works