我是Stack Overflow的新手,我需要您尝试帮助我解决这种情况。 我有一个使用 ReactPHP 在PHP下创建的服务器。
我有一个Java客户端,它可以正常连接到服务器并与之不断交换消息。
然后,我尝试使用Javascript中的 Socket.io 作为客户端与服务器以及Java中的服务器交换消息。 但是似乎有障碍阻止我这样做。 当我尝试使用Javascript连接客户端时,控制台中会显示此错误:
我的服务器(PHP):
<?php
use React\Socket\ConnectionInterface;
function console($s) {
echo $s . PHP_EOL;
}
require_once dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server("<address>:8083", $loop);
$socket->on('connection', function (ConnectionInterface $conn) {
console("[CONNECT] User " . $conn->getRemoteAddress() . " connected.");
/*
* I send a message to check that everything is okay.
* It seems that there is an error when I send this message
* and the client in Javascript will receive.
*/
$message = "Hello, client!";
$conn->write(json_encode([
"length" => strlen($message),
"message" => $message
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$conn->on('data', function ($chunk) use ($conn) {
console("[MESSAGE of " . $conn->getRemoteAddress() . "] " . trim($chunk));
});
});
console("Listening on " . $socket->getAddress() . "...");
$loop->run();
我的客户(JavaScript):
var socket = io("https://<address>:8083");
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
socket.on('connect', function(){
console.log('Connected');
});
socket.on('disconnect', function () {
console.log('Disconnected');
});
控制台输出:
注意:我使用Browserify在浏览器中运行Express,但这显然不是问题。 我以前使用Ratchet和Web Socket进行连接,但从未遇到任何问题。如果有人可以帮助我,请先谢谢您。