我想使用websockets。我将XAMPP与Windows 10一起使用。在<?php
$uri = 'http://myoldsite.net/data/';
$get = file_get_contents($uri);
$pos1 = strpos($get, "<div title=");
$pos2 = strpos($get, "</div>", $pos1);
$text = substr($get,$pos1,$pos2-$pos1+3);
echo $text;
?>
文件中,我取消注释了php.ini
行,因此我认为websockets应该是可用的。但我仍然得到错误;extension=php_sockets.dll
这是我的代码:
Fatal error: Uncaught Error: Call to undefined function socket_create()
我在浏览器端使用这个javascript:
<?php
error_reporting(E_ERROR);
set_time_limit (0);
$host = 'localhost';
$port = 1414;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $host, $port);
socket_listen($sock);
$sockets = array($sock);
$arClients = array();
while (true)
{
echo "Connecting...";
$sockets_change = $sockets;
$ready = socket_select($sockets_change, $write = null, $expect = null, null);
echo "Connected!";
foreach($sockets_change as $s)
{
if ($s == $sock)
{
$client = socket_accept($sock);
array_push($sockets, $client);
print_r($sockets);
}
else
{
$bytes = @socket_recv($s, $buffer, 2048, 0);
$string = implode(array_map("chr",$bytes));
if($string == ("hello world")){
socket_write($sock, "Hi");
}
}
}
}
?>
现在在页面加载时我得到<script type="text/javascript">
var ws_host = "localhost";
var ws_port = "1414";
var ws_server = "php_websockets_test.php";
var ws_url = "ws://" + ws_host + ":" + ws_port + "/" + ws_server;
var socket;
try{
socket = new WebSocket(ws_url);
socket.onopen = function(){
alert("Connected successfully!");
}
socket.onmessage = function(msg){
alert("Received: " + mgs);
}
socket.onclose = function(msg){
alert("Connection closed. " + msg);
}
} catch(ex){
alert("Exception: " + ex);
}
socket.send("hello world");
</script>
。那是为什么?