jessPHP Posted August 28, 2013 Share Posted August 28, 2013 Hi everyone!! I am developing a concurrent websocket server in PHP. I prefer to implement it using pthreads rather than using fork to create new processes, because the server is supposed to handle thousands of connections, and threads are lighter than processes. But when I execute this code: <?php class My extends Thread { public function run() { /** ... **/ } } $my = new My(); var_dump($my->start()); ?> I get this error: PHP Fatal error: Class 'Thread' not found in /home/user/htdocs/example/server.php on line 3 Why?? How can I get pthreads working in PHP?? By the way, do you have another suggestions to implement the concurrent server?? I am using Ubuntu 12.04 64bits Thank you very much! Quote Link to comment Share on other sites More sharing options...
kicken Posted August 28, 2013 Share Posted August 28, 2013 You'd need to build your own copy of PHP. Threads is not something that is standard with PHP, it is a PECL extension that you'd have to download and build. Look at the user notes on the installation page for some details. Depending on what exactly this server is supposed to do, you may not even need threads or processes to handle concurrent connections. Using non-blocking IO on the sockets will allow you to handle reading/writing multiple connections. You'd just have to make sure that processing for the sockets does not cause the script to block. Quote Link to comment Share on other sites More sharing options...
jessPHP Posted August 28, 2013 Author Share Posted August 28, 2013 Hi kicken, thank you very much for your response. I am not sure, but I think I need concurrent server because each client connection needs an unique communication. This would be my code: $client_sock = socket_accept($socket);$pid_child = pcntl_fork(); if($pid_child ==0){while(true){$input = socket_read($client_sock, 100);if($input) handle_message($input);elsebreak;} socket_close($client_sock);//send a message to the rest of clients, telling them that this client went offline} [code] I am developing an online game, where several users can invite other users to play. Besides, each user can send a private message to another user, something like a chat. Another thing I need, is to implement a shared list of users, using some IPC technique, but I do not know what to use... Thank you again! Quote Link to comment Share on other sites More sharing options...
krakjoe Posted August 30, 2013 Share Posted August 30, 2013 I'll assume you got as far as executing ... pthreads objects can be shared and manipulated among contexts, the problem of sharing a list of users between threads safely is a non-problem: <?php/* Note: this example is not well synchronized, sometimes numbers and data may mismatch*/class UserList extends Stackable { public function run(){}} class UserThread extends Thread { public function __construct(UserList $shared) { $this->shared = $shared; } public function run() { while (($num = count($this->shared))) { printf("%lu finds %s users\n", $this->getThreadId(), $num); var_dump($this->shared); /* randomly remove a user so this thread has an end */ if (time() % 2 == 0) $this->shared->shift(); /* sleep is bad, but you want to read output */ sleep(1); } }} /* create a list of users */$list = new UserList(); /* fill a list of users */$list->merge(array( "harry", "barry", "sue", "sally")); /* create some threads */$threads = array();$thread = 0; while ($thread < count($list)) { $threads[$thread] = new UserThread($list); $threads[$thread]->start(); $thread++;} /* you can still manipulate the list from here */$list[] = "larry";$list[] = "carry";$list[] = "krakjoe"; /* join some threads */foreach ($threads as $thread) $thread->join();?> In the example above you can see how an object can be manipulated by all contexts at the same time, hope that makes things clearer ... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 30, 2013 Share Posted August 30, 2013 Take a look at http://reactphp.org/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.