1

I have a php websocket server based on this one. This websocket server runs on a different apache than the public webserver but on the same physical server so I need a database connection to retrieve userdata. The userdata is fetched by the current session id which is also updated in the database when a user loggs in. Now, if I run my websocket server it works perfect but after a decent time (can't say a timespan) the result stays empty. I checked the dataset.

The querystring is up to date and the correct session id is in the database which should return the desired row but the result stays empty. If I kill the process and run this websocket server, it will work for the time being but after a while the result is empty again.

This is my websocketserver.php simplified:

set_time_limit(0);
require 'class.PHPWebSocket.php';

$db1 = mysql_connect( 'localhost', 'dev', 'xxxxxxxx' );
mysql_select_db('cb_dev',$db1);

function wsOnMessage($clientID, $message, $messageLength, $binary) {
    global $Server;

    $sessionid = "/*just imagine a session id here*/";
    $result = mysql_query("SELECT username, id FROM users WHERE sessionid = '$sessionid'");
    $row = mysql_fetch_row($result);
}

function wsOnOpen($clientID) {
    //
}

function wsOnClose($clientID, $status) {
    //
}

$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');

$Server->wsStartServer('0.0.0.0', 9010);

Note: The used session id is served correctly so this is not the problem.

1
  • Can you post the code that updates the session id when the user loggs in? Commented Jan 8, 2013 at 7:41

2 Answers 2

3

Swoole is a better choice for your requirement. It support native websocket server & async mysql in PHP ,with a c extension

WebSocket Server example:

$ws = new swoole_websocket_server("0.0.0.0", 9502);

$ws->on('open', function ($ws, $request) {
    var_dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "hello, welcome\n");
});

$ws->on('message', function ($ws, $frame) {
    echo "Message: {$frame->data}\n";
    $ws->push($frame->fd, "server: {$frame->data}");
});

$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();
Sign up to request clarification or add additional context in comments.

Comments

0

This is because your $sessionid was expired. Need to set higher timeout:

// 1 hour
ini_set('session.gc_maxlifetime', 3600);
session_set_cookie_params(3600);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.