2013-08-01 93 views
2

我正在爲我的項目使用websocket功能進行消息傳遞。我正在使用PHP websocket,我已經從以下鏈接下載鏈接 https://github.com/Flynsarmy/PHPWebSocket-Chat。但我的問題是我的網絡套接字自動斷開一段時間,並再次自動重新連接。因爲這個,我的信息正在迷失。那麼有人可以告訴我如何解決這個問題。代碼中是否存在任何修改,以便我可以解決此問題。謝謝。PHP websocket自動斷開連接

以下是我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) 
} 
} 
}; 
+0

請顯示您使用的代碼。 –

+0

我Patrik謝謝你的回覆。你可以從上面的鏈接獲得代碼表格。 – user2260521

+1

不,那是你從哪裏下載它的,我們需要你實際使用的代碼。除非我們看到您實際使用的代碼,否則我們無法幫助您解決您遇到的問題。只有相關的部分。 –

回答

2

我的猜測是,插座類沒有正確處理坪/ pongs。我之前使用過這個套接字腳本,並且回想起同樣的問題。

嘗試https://github.com/lemmingzshadow/php-websocket看看它是否是一個更好的起點。如果你只是試圖找出PHP中的websocket服務編程,這會變得更加複雜,但這是一個更好的開始任何一種方式的地方。

+0

嗨!感謝您的回覆,但您能否告訴我此Web套接字的可靠性,即它是否在收到消息時給出任何確認,或者至少在WebSocket斷開連接時記錄實例。 – user2260521

+0

@ user2260521 - 我無法對它的可靠性發表評論,因爲我沒有將它用作生產服務,但我使用它與其他幾個人一起對比了我爲PHP構建的websocket服務的性能使用Boost.Asio。我在回聲測試中打了數百萬次,並沒有崩潰或行爲超出我的預期。我不認爲它記錄到文件,但它有廣泛的控制檯日誌記錄,可能會使用輸出緩衝區捕獲並寫入文件。 – JSON