PHP websocket自动断开连接

时间:2013-08-01 09:25:36

标签: php websocket phpwebsocket

我正在为项目使用websocket功能进行消息传递。我正在使用PHP websocket,我从以下链接下载  https://github.com/Flynsarmy/PHPWebSocket-Chat。但我的问题是我的网络套接字自动断开一段时间后再次自动重新连接。因此,我的消息正在迷失。那么任何人都可以告诉我如何解决这个问题。我可以做的代码中有任何修改,以便我可以克服这个问题。谢谢。

以下是我的server.php文件

    <?php
// prevent the server from timing out
set_time_limit(0);

// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';

// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}

//The speaker is the only person in the room. Don't let them feel lonely.
if ( sizeof($Server->wsClients) == 1 )
$Server->wsSend($clientID, "There isn't anyone else in the room, but I'll still listen to you. --Your Trusty Server");
else
//Send the message to everyone but the person who said it
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) said \"$message\"");
}

// when a client connects
function wsOnOpen($clientID)
{
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

$Server->log( "$ip ($clientID) has connected." );

//Send a join notice to everyone but the person who joined
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) has joined the room.");
}

// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

$Server->log( "$ip ($clientID) has disconnected." );

//Send a user left notice to everyone in the room
foreach ( $Server->wsClients as $id => $client )
$Server->wsSend($id, "Visitor $clientID ($ip) has left the room.");
}

// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('127.0.0.1', 9300);

?>

javascript代码是

    var FancyWebSocket = function(url)
{
var callbacks = {};
var ws_url = url;
var conn;

this.bind = function(event_name, callback){
callbacks[event_name] = callbacks[event_name] || [];
callbacks[event_name].push(callback);
return this;// chainable
};

this.send = function(event_name, event_data){
this.conn.send( event_data );
return this;
};

this.connect = function() {
if ( typeof(MozWebSocket) == 'function' )
this.conn = new MozWebSocket(url);
else
this.conn = new WebSocket(url);

// dispatch to the right handlers
this.conn.onmessage = function(evt){
dispatch('message', evt.data);
};

this.conn.onclose = function(){dispatch('close',null)}
this.conn.onopen = function(){dispatch('open',null)}
};

this.disconnect = function() {
this.conn.close();
};

var dispatch = function(event_name, message){
var chain = callbacks[event_name];
if(typeof chain == 'undefined') return; // no callbacks for this event
for(var i = 0; i < chain.length; i++){
chain[i]( message )
}
}
};

1 个答案:

答案 0 :(得分:2)

我的猜测是套接字类没有正确处理ping / pongs。我之前使用过那个套接字脚本并回想起有同样的问题。

尝试https://github.com/lemmingzshadow/php-websocket,看看它是否是一个更好的起点。如果你只是试图在PHP中找出websocket服务编程,那就更复杂了,但这是一个更好的起点。