尝试在JS到PHP服务器中创建与websocket的连接, 无法连接到服务器但没有抛出异常
JS
try {
var socket;
var host = "ws://www.somecoolsite.com:9080/test.php";
var socket = new WebSocket(host);
console.log("opening socket...");
socket.onopen = function () {
console.log('Socket Status: ' + socket.readyState + ' (open)');
socket.send("2");
console.log("sent first package");
}
socket.onconsole.log = function (msg) {
console.log('Received: ' + msg.data);
}
socket.onclose = function () {
console.log('Socket Status: ' + socket.readyState + ' (Closed)');
}
} catch (exception) {
console.log('Error' + exception);
}
PHP on somecoolsite
$server = stream_socket_server("tcp://localhost:9080", $errno, $errorMessage);
if ($server === false) {
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
} else {
echo "Working";
}
for (;;) {
$client = @stream_socket_accept($server);
if ($client) {
stream_copy_to_stream($client, $client);
fclose($client);
}
}
我一整天都陷入困境,无法建立套接字连接。任何人都知道这里可能出现什么问题?
答案 0 :(得分:0)
请删除此部分代码
socket.onconsole.log = function (msg) {
console.log('Received: ' + msg.data);
}
你不能做" socket.onconsole.log"
在我的控制台中,我获得了一个" Socket statut 3(已关闭)"
请查看此链接