使用php在android中使用websocket实现聊天
我想用php在android中使用websocket实现聊天功能 我需要相同的php代码 场景是这样的 android开发者将从android设备向php应用程序发送消息,而不是从php发送消息,消息将发送到另一个用户的android手机 之后,我需要保存数据库中的所有聊天以及 我不知道怎么做 请帮助并提供此代码 我写的代码如下使用php在android中使用websocket实现聊天,php,android,websocket,chat,Php,Android,Websocket,Chat,我想用php在android中使用websocket实现聊天功能 我需要相同的php代码 场景是这样的 android开发者将从android设备向php应用程序发送消息,而不是从php发送消息,消息将发送到另一个用户的android手机 之后,我需要保存数据库中的所有聊天以及 我不知道怎么做 请帮助并提供此代码 我写的代码如下 $address = "0.0.0.0"; $port = 5000; $max_clients = 10; if(!($sock = socket_create(A
$address = "0.0.0.0";
$port = 5000;
$max_clients = 10;
if(!($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($sock, $address , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$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
$client_socks = array();
//array of sockets to read
$read = array();
//start loop to listen for incoming connections and process existing connections
while (true)
{
//prepare array of readable client sockets
$read = array();
//first socket is the master socket
$read[0] = $sock;
//now add the existing client sockets
for ($i = 0; $i < $max_clients; $i++)
{
if($client_socks[$i] != null)
{
$read[$i+1] = $client_socks[$i];
}
}
//now call select - blocking call
if(socket_select($read , $write , $except , null) === false)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read))
{
for ($i = 0; $i < $max_clients; $i++)
{
if ($client_socks[$i] == null)
{
$client_socks[$i] = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client_socks[$i], $address, $port))
{
echo "Client $address : $port is now connected to us. \n";
}
//Send Welcome message to client
$message = "Welcome to php socket server version 1.0 \n";
$message .= "Enter a message and press enter, and i shall reply back \n";
socket_write($client_socks[$i] , $message);
break;
}
}
}
//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{
if (in_array($client_socks[$i] , $read))
{
$input = socket_read($client_socks[$i] , 1024);
if ($input == null)
{
//zero length string meaning disconnected, remove and close the socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
}
$n = trim($input);
$output = "OK ... $input";
//echo "Sending output to client \n";
//send response to client
//socket_write($client_socks[1] , $output);
//send response to client
socket_write($client_socks[$i] , $output);
}
}
}
$address=“0.0.0.0”;
$port=5000;
$max_客户=10;
if(!($sock=socket\u create(AF\u INET,sock\u STREAM,0)))
{
$errorcode=socket\u last\u error();
$errormsg=socket\u strerror($errorcode);
die(“无法创建套接字:[$errorcode]$errormsg\n”);
}
回显“已创建套接字\n”;
//绑定源地址
如果(!socket_bind($sock,$address,5000))
{
$errorcode=socket\u last\u error();
$errormsg=socket\u strerror($errorcode);
die(“无法绑定套接字:[$errorcode]$errormsg\n”);
}
回显“套接字绑定正常\n”;
如果(!socket_listen($sock,10))
{
$errorcode=socket\u last\u error();
$errormsg=socket\u strerror($errorcode);
die(“无法侦听套接字:[$errorcode]$errormsg\n”);
}
回显“套接字侦听正常\n”;
回显“正在等待传入连接…\n”;
//客户端套接字数组
$client_socks=array();
//要读取的套接字数组
$read=array();
//启动循环以侦听传入连接并处理现有连接
while(true)
{
//准备可读的客户端套接字数组
$read=array();
//第一个插座是主插座
$read[0]=$sock;
//现在添加现有的客户端套接字
对于($i=0;$i<$max\u客户端;$i++)
{
如果($client_socks[$i]!=null)
{
$read[$i+1]=$client_socks[$i];
}
}
//现在调用select-阻塞调用
if(socket_select($read,$write,$except,null)==false)
{
$errorcode=socket\u last\u error();
$errormsg=socket\u strerror($errorcode);
die(“无法侦听套接字:[$errorcode]$errormsg\n”);
}
//如果ready包含主套接字,则新的连接已进入
if(在数组中($sock,$read))
{
对于($i=0;$i<$max\u客户端;$i++)
{
如果($client_socks[$i]==null)
{
$client_socks[$i]=socket_accept($sock);
//显示有关已连接的客户端的信息
if(socket\u getpeername($client\u socks[$i],$address,$port))
{
echo“客户端$address:$端口现在已连接到我们。\n”;
}
//向客户发送欢迎信息
$message=“欢迎使用php套接字服务器1.0版\n”;
$message.=“输入消息并按Enter键,我将回复\n”;
socket_write($client_socks[$i],$message);
打破
}
}
}
//检查每个客户端是否发送任何数据
对于($i=0;$i<$max\u客户端;$i++)
{
if(在数组中($client\u socks[$i],$read))
{
$input=socket\u read($client\u socks[$i],1024);
如果($input==null)
{
//零长度字符串表示断开连接,请卸下并关闭插座
未结算($client_socks[$i]);
插座关闭($client_socks[$i]);
}
$n=修剪($input);
$output=“确定…$input”;
//回显“将输出发送到客户端\n”;
//向客户端发送响应
//socket_write($client_socks[1],$output);
//向客户端发送响应
socket_write($client_socks[$i],$output);
}
}
}
检查此链接
Ratchet是一个松散耦合的PHP库,为开发人员提供了通过WebSocket在客户端和服务器之间创建实时、双向应用程序的工具。您有任何解决方案吗,请告诉我我们需要相同的解决方案。