在PHP网站上的example之后,我能够编写一个简单的脚本并将其放在服务器上,然后通过命令<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-2 col-sm-1 col-md-1 col-lg-2">
<div>
<a>Some codes</a>
</div>
</div>
<div class="col-8 col-sm-9 col-md-9 col-lg-8">
<div>
<nav>
<ul class="cf--main-menu-wrapper">
<li><a href="#">Home</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Our Features</a></li>
</ul>
</nav>
</div>
</div>
<div class="col-2 col-sm-2 col-md-2 col-lg-2">
<div>
<a>Download App</a>
</div>
</div>
</div>
</div>
连接到它。 PHP代码是:
telnet example.com 10000
当我连接telnet时,它运行良好。无论在终端机中键入什么内容,我都会被发回。但是
但是,我无法通过JavaScript连接到它。我在MDN中使用的代码如下:
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '<server-ip-address>';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
在Firefox控制台中的结果是:
var exampleSocket = new WebSocket("wss://example.com:10000");
我还尝试了undefined
Firefox can’t establish a connection to the server at wss://example.com:10000/.
版本。我收到错误“无法建立与服务器的安全连接”。请注意,我已经在网站上启用了ssl,因此我认为它应该能够安全地连接。
有什么想法可以使用JavaScript连接到上面的PHP代码吗?