Amazon ec2 WebSocket在Amazon ec2环境中不起作用
我通过websockets制作了一个聊天应用程序,并利用了这个链接“”。虽然它在我自己的开发服务器上运行良好,但当我在公司的amazon ec2服务器上托管它时,它显示出错误: “Firefox无法在ws://external ip for amazon:port/上建立与服务器的连接” 我在尝试将客户端连接到服务器时使用服务器的外部IP,在执行服务器文件时使用服务器的内部IP for amazon。我还在ec2中加入了一个转发规则,使我的端口可以通过tcp和udp访问 我还检查了我的亚马逊服务器的ELB,我的服务器没有ELB 但它仍然不起作用。这是我的服务器文件代码的一部分,其中创建套接字并连接客户端Amazon ec2 WebSocket在Amazon ec2环境中不起作用,amazon-ec2,websocket,phpwebsocket,Amazon Ec2,Websocket,Phpwebsocket,我通过websockets制作了一个聊天应用程序,并利用了这个链接“”。虽然它在我自己的开发服务器上运行良好,但当我在公司的amazon ec2服务器上托管它时,它显示出错误: “Firefox无法在ws://external ip for amazon:port/上建立与服务器的连接” 我在尝试将客户端连接到服务器时使用服务器的外部IP,在执行服务器文件时使用服务器的内部IP for amazon。我还在ec2中加入了一个转发规则,使我的端口可以通过tcp和udp访问 我还检查了我的亚马逊服务
if (isset($this->wsRead[0])) return false;
if (!$this->wsRead[0] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
return false;
}
if (!socket_set_option($this->wsRead[0], SOL_SOCKET, SO_REUSEADDR, 1)) {
socket_close($this->wsRead[0]);
return false;
}
if (!socket_bind($this->wsRead[0], $host, $port)) {
socket_close($this->wsRead[0]);
return false;
}
if (!socket_listen($this->wsRead[0], 10)) {
socket_close($this->wsRead[0]);
return false;
}
$write = array();
$except = array();
$nextPingCheck = time() + 1;
while (isset($this->wsRead[0])) {
$changed = $this->wsRead;
$result = socket_select($changed, $write, $except, 1);
if ($result === false) {
socket_close($this->wsRead[0]);
return false;
}
elseif ($result > 0) {
foreach ($changed as $clientID => $socket) {
if ($clientID != 0) {
// client socket changed
$buffer = '';
$bytes = @socket_recv($socket, $buffer, 4096, 0);
//$this->log($buffer);
if ($bytes === false) {
// error on recv, remove client socket (will check to send close frame)
$this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
}
elseif ($bytes > 0) {
// process handshake or frame(s)
//$this->log("hi".$buffer);
if (!$this->wsProcessClient($clientID, $buffer, $bytes)) {
$this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
}
}
else {
// 0 bytes received from client, meaning the client closed the TCP connection
$this->wsRemoveClient($clientID);
}
}
else {
// listen socket changed
$client = socket_accept($this->wsRead[0]);
if ($client !== false) {
// fetch client IP as integer
$clientIP = '';
$result = socket_getpeername($client, $clientIP);
//$this->log($clientIP);
$clientIP = ip2long($clientIP);
//$this->log($clientIP);
if ($result !== false && $this->wsClientCount < self::WS_MAX_CLIENTS && (!isset($this->wsClientIPCount[$clientIP]) || $this->wsClientIPCount[$clientIP] < self::WS_MAX_CLIENTS_PER_IP)) {
$this->wsAddClient($client, $clientIP);
}
else {
socket_close($client);
}
}
}
}
}
if (time() >= $nextPingCheck) {
$this->wsCheckIdleClients();
$nextPingCheck = time() + 1;
}
}
return true; // returned when wsStopServer() is called
if(isset($this->wsRead[0])返回false;
如果(!$this->wsRead[0]=socket\u创建(AF\u INET、SOCK\u STREAM、SOL\u TCP)){
返回false;
}
if(!socket\u set\u option($this->wsRead[0],SOL\u socket,SO\u REUSEADDR,1)){
socket_close($this->wsRead[0]);
返回false;
}
如果(!socket_bind($this->wsRead[0],$host,$port)){
socket_close($this->wsRead[0]);
返回false;
}
如果(!socket_listen($this->wsRead[0],10)){
socket_close($this->wsRead[0]);
返回false;
}
$write=array();
$except=array();
$nextPingCheck=time()+1;
而(isset($this->wsRead[0])){
$changed=$this->wsRead;
$result=socket\u select($changed,$write,$except,1);
如果($result==false){
socket_close($this->wsRead[0]);
返回false;
}
elseif($result>0){
foreach($更改为$clientID=>$socket){
如果($clientID!=0){
//客户端套接字已更改
$buffer='';
$bytes=@socket\u recv($socket,$buffer,4096,0);
//$this->log($buffer);
如果($bytes==false){
//recv错误,删除客户端套接字(将检查以发送关闭帧)
$this->wsSendClientClose($clientID,self::WS\u STATUS\u PROTOCOL\u ERROR);
}
elseif($bytes>0){
//处理握手或帧
//$this->log(“hi”。$buffer);
if(!$this->wsProcessClient($clientID、$buffer、$bytes)){
$this->wsSendClientClose($clientID,self::WS\u STATUS\u PROTOCOL\u ERROR);
}
}
否则{
//从客户端接收到0字节,表示客户端已关闭TCP连接
$this->wsRemoveClient($clientID);
}
}
否则{
//侦听套接字已更改
$client=socket_accept($this->wsRead[0]);
如果($client!==false){
//将客户端IP提取为整数
$clientIP='';
$result=socket\u getpeername($client,$clientIP);
//$this->log($clientIP);
$clientIP=ip2long($clientIP);
//$this->log($clientIP);
if($result!==false&&$this->wsClientCountwsClientIPCount[$clientIP])|this->wsClientIPCount[$clientIP]wsAddClient($client,$clientIP);
}
否则{
socket_close($client);
}
}
}
}
}
如果(time()>=$nextPingCheck){
$this->wsCheckIdleClients();
$nextPingCheck=time()+1;
}
}
返回true;//调用wsStopServer()时返回
我认为这个问题与socket_select()函数有关,该函数无法选择创建的套接字。
如何让它工作?
请帮助我,因为我无法解决这个问题,即使经过多次尝试
更新
我发现这个问题是由amazon上旧版本的python引起的。因此,我将python版本更改为2.6/2.7
我还替换了socket.io库来运行聊天。工作非常顺利。您使用的WebSocket实现似乎实现了不推荐的Hixie-76版本的WebSocket协议,Firefox(以及Chrome和其他浏览器)不再支持该协议
我建议看一看。这是最新的,经过了很好的测试。Hi oberstet。谢谢你的回复。但我的项目是在本地主机和我的开发服务器上进行的。在chrome、firefox和ie10上。它只是不适用于AmazonEC2。另外,我必须从头开始编写代码。我不能使用任何我看不见的外部库。你在使用端口吗