我们在运行apache和php5的专用服务器上使用websocket算法,一切正常,除非有时我们遇到错误导致echo服务器崩溃。算法中发生了3个错误(这段代码中的注释):
$numBytes = @socket_recv($socket,$buffer,$this->maxBufferSize,0);
if ($numBytes === false)
{
/* 110 : Connection timed out */
/* 113 : No route to host */
/* 104 : Connection reset by peer */
$error_msg = socket_strerror(socket_last_error($socket));
socket_close($this->master);
throw new Exception('Socket error: ' . $error_msg);
}
此代码部分在无限的时间内。我不粘贴所有东西,没有任何东西,以保持帖子干净,但随时问我更多的代码。有谁知道为什么会发生这些错误,有没有办法让它们不会崩溃整个脚本?
这是服务器启动的方式:
$this->maxBufferSize = 2048;
$this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Failed: socket_create()");
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option() (reuse address)");
socket_set_option($this->master,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>100)) or die("Failed: socket_option() (timeout)");
socket_bind($this->master, $addr, $port) or die("Failed: socket_bind()");
socket_listen($this->master) or die("Failed: socket_listen()");
$this->sockets['m'] = $this->master;