【问题标题】:PHP websocket disconnects automaticallyPHP websocket 自动断开连接
【发布时间】:2013-08-02 03:25:48
【问题描述】:

我正在为我的项目使用 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 )
}
}
};

【问题讨论】:

  • 请出示您正在使用的代码。
  • Patrik 感谢您的回复。您可以在链接 github.com/Flynsarmy/PHPWebSocket-Chat>. 上方获取代码表单
  • 不,您从那里下载它,我们需要您实际使用的代码。除非我们看到您实际使用的代码,否则我们无法帮助您解决您遇到的问题。而且只有相关部分。
  • 嗨,Patrik,你能检查一下代码吗?
  • 还有 javascript 代码?

标签: php websocket phpwebsocket


【解决方案1】:

我的猜测是套接字类没有正确处理 pings/pongs。我以前使用过那个套接字脚本,并记得有同样的问题。

试试https://github.com/lemmingzshadow/php-websocket,看看它是否是一个更好的起点。如果您只是想在 PHP 中进行 websocket 服务编程,那就更复杂了,但无论哪种方式,它都是一个更好的起点。

【讨论】:

  • 嗨!谢谢你的回复,但你能告诉我这个网络套接字的可靠性,即它是否在收到消息时给予任何确认,或者至少在网络套接字断开连接时记录实例。
  • @user2260521 - 我无法评论它的可靠性,因为我没有将它用作生产服务,但我使用它和其他一些服务来比较 websocket 服务的性能我正在使用 Boost.Asio 为 PHP 构建。我在回声测试中击中了它数百万次,它没有崩溃或表现超出我的预期。我不认为它记录到文件中,但它有大量的控制台日志记录,可以使用输出缓冲区捕获并写入文件。
猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 2019-06-18
  • 2020-02-07
  • 2019-05-31
相关资源
最近更新 更多