PHPを使用しているオンラインのほとんどのHTML5 Webソケットのサンプルがソケットプラグインを使用しているようです。 HTML5ウェブソケットにstream_socket_server
を使用することはできますか?PHPストリームソケットがHTML5ソケットで動作しませんか?
もしそうなら、PHP stream_socket_server
の機能を持つ単純なソケットサーバー+クライアントを構築しようとしています。
PHPソケットサーバ:
<?php
$server = stream_socket_server("tcp://localhost:8080", $errno, $errorMessage);
if ($server === false) {
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}
for (;;) {
$client = stream_socket_accept($server);
if ($client) {
echo 'Connection accepted from '.stream_socket_get_name($client, false) . "\n";
stream_copy_to_stream($client, $client);
}
}
HTML5のWebソケットクライアントここでは、コードです。私が見逃している何
WebSocket connection to 'ws://localhost:8080/' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Client Testing</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
</head>
<body>
<button id="send">Testing button</button>
<script>
websocket = new WebSocket("ws://localhost:8080");
websocket.onopen = function(evt) { /* do stuff */ }; //on open event
websocket.onclose = function(evt) { /* do stuff */ }; //on close event
websocket.onmessage = function(evt) { /* do stuff */ }; //on message event
websocket.onerror = function(evt) { /* do stuff */ }; //on error event
$('#send').click(function(){
websocket.send("This is a testing message"); //send method
});
//websocket.close(); //close method
</script>
</body>
</html>
そして、これは私が接続していリターンがありますか?有効なHTTP応答を返すにはどうすればよいですか?
それはあなたがすべてのWebSocketのRFCを次されていない、オープンソースのテストライブラリに固執する方が良いでしょう。 – Gntem