错误信息:WebSocket connection to 'ws://127.0.0.1:8888/' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
请教下这是啥情况咋解决
service代码
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
echo 'server start';
if(socket_bind($socket,"127.0.0.1",8888)==false){
echo 'server bind fail:'.socket_strerror(socket_last_error());
}
if(socket_listen($socket,0) == false){
echo 'server listen fail:'.socket_strerror(socket_last_error());
}
while (true){
$accept=socket_accept($socket);
if($accept!=false){
$string=socket_read($accept,1024);
echo 'server receive is :'.$string.PHP_EOL;//PHP_EOL为php的换行预定义常量
if($string!=false){
$return_string='server receive is :'.$string.PHP_EOL;
$ret = socket_write($accept,$return_string,strlen($return_string));
var_dump($ret);
}else{
echo 'socket_read is fail';
}
socket_close($accept);
}
}
socket_close($socket);
client
<script>
var ws = new WebSocket("ws://127.0.0.1:8888");
ws.onopen = function (event) {
console.log("open");
ws.send("hello");
};
ws.onmessage = function (p1) {
console.log(p1)
};
ws.onerror = function(evt)
{
console.log("WebSocketError!");
console.log(evt);
};
</script>
繁花如伊
慕的地6264312
有只小跳蛙