現在のソケットにデータ (テキスト) を送信しようとしています。
握手をする前に - OK。
その後、クライアント(Chrome、最新バージョン)からデータを送信 -socket_recv
受信 - OK。
socket_recv
次に、何も使用して読み取ったものと同じソケットに書き込もうとしていますsocket_write
。
すべてのエラー ( を使用してチェックsocket_last_error
され、関数が を返しますTRUE
)。すべてOKのようです。
しかしonmessage
、クライアント側のイベントは発生しません。沈黙...
PHP で websocket を使用していた方、助けてください。
クライアント:
var host = "ws://192.168.0.100:5000/wbs_r1.php";
try {
var socket = new WebSocket(host);
message('<p class="event">Socket Status: ' + socket.readyState);
socket.onopen = function (msg) {
message('<p class="event">Socket Status: ' + socket.readyState + ' (open)');
console.log(socket);
console.log(msg);
}
socket.onmessage = function (msg) {
message('<p class="message">Received: ' + msg.data);
console.log(socket);
console.log(msg);
}
socket.onclose = function (msg) {
message('<p class="event">Socket Status: ' + socket.readyState + ' (Closed)');
console.log(socket);
console.log(msg);
}
socket.onerror = function (msg) {
message('Error: ');
console.log(socket);
console.log(msg);
}
} catch (exception) {
message('<p>Error' + exception);
console.log(exception);
}
サーバ:
header('Content-Type: text/plain; charset=utf-8');
set_time_limit(0);
error_reporting(E_ALL);
ob_implicit_flush(true);
$sockets = array();
$handshakes = array();
$sockets["wbs1"] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($sockets["wbs1"], SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sockets["wbs1"], "0.0.0.0", 5000);
socket_listen($sockets["wbs1"], 20);
socket_set_nonblock($sockets["wbs1"]);
echo $sockets["wbs1"] . "\n";
while(true) {
$changed_sockets = $sockets;
$num_sockets = socket_select($changed_sockets, $write = null, $exceptions = null, null);
//print_r($changed_sockets);
foreach($changed_sockets as $socket) {
echo "\n\n\n" . "Current socket: " . $socket . "\n";
if($socket == $sockets["wbs1"]) {
if(($client = socket_accept($sockets["wbs1"])) >= 0) $sockets[] = $client;
echo "Accepting socket ".$socket."\n";
echo "Client: " . $client . "\n";
} else {
$index = array_search($socket, $sockets);
$len = @socket_recv($socket, $buffer, 2048, 0);
echo "Received " . $len . " bytes from socket ".$socket.": " . $buffer . "\n";
if($len == 0) {
unset($sockets[$index]);
unset($handshakes[$index]);
socket_close($socket);
echo "Closing socket " . $socket . "\n";
} else if(!isset($handshakes[$index])) {
$buffer = substr($buffer, strpos($buffer, "Sec-WebSocket-Key: ") + 19);
$accept = base64_encode(sha1(substr($buffer, 0, strpos($buffer, "\r\n")) . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
$upgrade = "HTTP/1.1 101 Switching Protocols\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: {$accept}\r\n\r\n" .
"Sec-Websocket-Extensions=x-webkit-deflate-frame". chr(0);
socket_write($socket, $upgrade, strlen($upgrade));
$handshakes[$index] = true;
echo "Handshaking: " . $socket . "\n";
} else {
$msg = "Hello!!!\n\0";
$wr = socket_write($socket,$msg);
if(!$wr) echo socket_strerror(socket_last_error()) . "\n";
else echo "Writing to socket " . $socket . "\n";
}
}
}
}