2013-08-01 4 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); 

?> 

입니다 그리고 자바 스크립트 코드는

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에서 웹 소켓 서비스 프로그래밍을 이해하려고 시도하는 것이 더 복잡하지만 어느 쪽이든 시작하는 것이 더 좋습니다.

+0

안녕하세요! 답장을 보내 주셔서 감사합니다. 그러나이 웹 소켓의 신뢰성을 알려주시겠습니까? 즉, 웹 소켓이 연결을 끊었을 때 메시지가 수신되거나 적어도 인스턴스를 기록 할 때 어떤 확인을 제공합니까? – user2260521

+0

@ user2260521 - 프로덕션 서비스로 사용하지 않았기 때문에 얼마나 신뢰할 수 있는지에 대해서는 언급 할 수 없지만 PHP를 위해 구축중인 websocket 서비스와 성능을 비교하기 위해 다른 제품과 함께 사용했습니다 Boost.Asio를 사용합니다. 나는 에코 테스트에서 수백만 배의 시간을 쳤고, 예상했던 것보다 크래시 나 행동을하지 않았다. 나는 그것이 파일에 기록 된 것 같지 않지만 출력 버퍼를 사용하여 잡힐 수 있고 파일에 기록 될 수있는 광범위한 콘솔 로깅을 가졌다. – JSON

관련 문제