我正在尝试使用套接字制作一个用于语音聊天的 php 服务器。我可以向客户端发送纯数据,例如 html 等。但我想发送语音数据,我首先使用 mp3 文件对其进行测试。我的服务器代码:
<?php
error_reporting(0);
set_time_limit(0);
define('IP', '192.168.56.1');
define('PORT', '5000');
define('MAX_CLIENT', 10);
require_once 'lib/socket.php';
$socket = new socket();
$socket->run();
while (TRUE){
$socket->accept_multi_client();
$socket->speak();
}
?>
和我的服务器类:
<?php
class socket {
public static $address = IP;
public static $port = PORT;
public static $max_clients = MAX_CLIENT;
public static $client_socks;
public static $sock;
public static $read;
function run() {
if (!(socket::$sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if (!socket_bind(socket::$sock, socket::$address, socket::$port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if (!socket_listen(socket::$sock, socket::$max_clients)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//array of client sockets
socket::$client_socks = array();
}
function accept_multi_client() {
//prepare array of readable client sockets
socket::$read = array();
//first socket is the master socket
socket::$read[0] = socket::$sock;
//now add the existing client sockets
for ($i = 0; $i < socket::$max_clients; $i++) {
if (socket::$client_socks[$i] != null) {
socket::$read[$i + 1] = socket::$client_socks[$i];
}
}
//now call select - blocking call
if (socket_select(socket::$read, $write, $except, null) === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
print("Could not listen on socket : [$errorcode] $errormsg \n");
}
//if ready contains the master socket, then a new connection has come in
if (in_array(socket::$sock, socket::$read)) {
for ($i = 0; $i < socket::$max_clients; $i++) {
if (socket::$client_socks[$i] == null) {
socket::$client_socks[$i] = socket_accept(socket::$sock);
//display information about the client who is connected
if (socket_getpeername(socket::$client_socks[$i], socket::$address, socket::$port)) {
echo "Client $address : $port is now connected to us. \n";
}
//Send Welcome message to client
/*$message = "\n";
//$message .= "Enter a message and press enter, and i shall reply back \n";
socket_write(socket::$client_socks[$i], $message);*/
break;
}
}
}
}
function speak() {
for ($i = 0; $i < socket::$max_clients; $i++) {
if (in_array(socket::$client_socks[$i], socket::$read)) {
$input = socket_read(socket::$client_socks[$i], 1024);
echo $input;
/* if ($input == null) {
//zero length string meaning disconnected, remove and close the socket
unset(socket::$client_socks[$i]);
socket_close(socket::$client_socks[$i]);
}
*/
$output = $this->do_anything($input, $i);
// echo $output;
//send response to client
socket_write(socket::$client_socks[$i], $output);
socket_close(socket::$client_socks[$i]);
}
}
}
function do_anything($input, $i) {
$filename = 'ee.mp3';
ob_start();
//ob_end_clean();
if (file_exists($filename)) {
print('HTTP/1.1 200 OK'."\n");
print('Content-Type: audio/mpeg'." ");
print('Content-Disposition: filename="test.mp3"'."\n");
print('Content-length: ' . 1129297 ."\n");
/*print('Cache-Control: no-cache'." ");
print("Content-Transfer-Encoding: chunked"." ");*/
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
} else {
print("HTTP/1.0 404 Not Found");
}
$out = ob_get_contents();
//ob_end_clean();
return $out.$contents;
}
}
?>
我可以使用 wget 获取 mp3 文件并使用 mplayer 播放。但我想在网上用 html5 播放器玩这个。
这不适用于我的服务器:
<audio controls>
<source src="http://192.168.56.1:5000/" type="audio/mpeg">
</audio>