【发布时间】:2020-04-03 09:34:20
【问题描述】:
我是使用 php web sockets 的新手。我正在尝试实现简单的 Web 套接字,但我收到了绑定地址的错误。我尝试更改端口号,但在我的情况下不起作用。我正在我的本地机器上尝试这个。 代码如下
<?php
$host ="localhost";
$port =12345;
set_time_limit(0);
$sock = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket \n");
$result_sock = socket_bind($sock,$host,$port)or die("Could not bind to socket \n");
$result_sock = socket_listen($sock,3)or die("Could not Setup socket listener \n");
echo "Listening for connections";
do{
$accept = socket_accept($sock)or die("Could not accept incoming connection \n");
$msg = socket_read($accept,1024)or die ("Could not read input \n");
socket_write($accept,$reply,strlen($reply)) or die ("Could not write input \n");
$errors = array();
// connect db
$db= mysqli_connect('localhost','root','','color_luck_db') or die ("Could not connect to DATABASE");
$timer_value = "SELECT Timer_Value from gamehistory where Room_Id= '1'";
$result_timer= mysqli_query($db,$timer_value);
$_SESSION['timer_value']= $results;
$result_timer=$result_timer--;
}while($result_timer>0);
socket_close($accept,$sock);
?>
【问题讨论】:
-
此代码用于socket.php文件
标签: php sockets websocket phpwebsocket