0
서버 측을받지 않습니다웹 소켓은 메시지를
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '127.0.0.1', 8000);
socket_listen($socket);
$client = socket_accept($socket);
$handshake = socket_read($client, 1024);
preg_match('/Sec-WebSocket-Key\: (.+?)\r\n/', $handshake, $accept);
$accept = base64_encode(sha1("$accept[1]258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
$handshake = "HTTP/1.1 101 Switching Protocols\r\n";
$handshake .= "Upgrade: websocket\r\n";
$handshake .= "Connection: Upgrade\r\n";
$handshake .= "Sec-WebSocket-Accept: $accept\r\n\r\n";
socket_write($client, $handshake.chr(0), strlen($handshake.chr(0)));
socket_write($client, 's'.chr(0));
sleep(5);
socket_close($client);
socket_close($socket);
?>
클라이언트 측 : 나는 "소켓을 열고"5 초 후에 "가까운 소켓"얻을
<div id="output"></div>
<script>
if ("WebSocket" in window) {
var socket = new WebSocket('ws://127.0.0.1:8000');
socket.onopen = function() {
document.getElementById('output').innerHTML += 'socket open<br />';
}
socket.onmessage = function(msg) {
document.getElementById('output').innerHTML += 'new msg: '+msg.data+'<br />';
}
socket.onclose = function() {
document.getElementById('output').innerHTML += 'socket close';
}
}
</script>
합니다. 나는 서버에 의해 보내진 "s"를 얻지 못한다.
Firefox 및 Chrome에서 사용해 봤습니다.
내가 뭘 잘못하고 있니? 클라이언트와 서버 사이의
덕분에, 그러나 나는 시도하고 작동하지 않습니다 [링크] (http://pastebin.com/X6DJg3b0). –
자바 스크립트에서 onerror 콜백을 추가하여 실행되지 않는 것을 확인할 수 있습니까? server-side send()에서 base64_encode를 제거 할 수도 있지만 코드가 단순화되지만 문제는 도움이되지 않습니다. 'WS에 대한 연결 : – simonc
내가 불을 지르고에서 오류를 볼 수 있습니다 //127.0.0.1 : 페이지 loading.'가있는 동안 8000/중단되었습니다. GC의 개발자 도구에서 :'서버는 클라이언트에게 보내는 프레임을 마스크해서는 안됩니다 .'. 그리고 그 오류가 발생하면 소켓이 닫힙니다. * 오류가 발생하면 onerror가 호출됩니다. –