このリンク " http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/ "を利用して、websockets を介してチャット アプリケーションを作成しました。自分の開発サーバーでは完全に機能していますが、会社の amazon ec2 サーバーでホストすると、次のエラーが表示されます:「Firefox は、amazon:port/ の ws://external ip でサーバーへの接続を確立できません。 "
クライアントをサーバーに接続しようとするときにサーバーの外部 IP を使用し、サーバー ファイルを実行するときに Amazon のサーバーの内部 IP を使用しています。また、ec2 に転送ルールを設定して、UDP だけでなく tcp でもポートにアクセスできるようにしました。
また、Amazon サーバーの ELB を確認しましたが、サーバーに ELB が配置されていません。
それでも機能しません。これは、ソケットが作成され、クライアントが接続されるサーバーのファイルのコードの一部です。
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
この問題は、作成されたソケットを選択できない socket_select() 関数を中心に展開していると思います。それを機能させる方法は?何度試してもこの問題を解決できないので、助けてください。
アップデート
この問題は、Amazon の古いバージョンの Python が原因で発生していることがわかりました。そこで、python のバージョンを 2.6/2.7 に変更しました
また、チャットを実行するために socket.io ライブラリを置き換えました。その作業は非常にスムーズです。