gnope php gtk gui gnope.org  

Server Loop

 
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners
View previous topic :: View next topic  
Author Message
madsjuul



Joined: 22 Jan 2007
Posts: 3

PostPosted: Mon Jan 22, 2007 11:34 am    Post subject: Server Loop Reply with quote

Hey I'm completely new to PHP-Gtk
But have done a little PHP programming
I have created A PHP-CLI Socket Server which listens to a port In a While Loop

now I want to Put this Server Into a GUI. but Which Loop can I connect it too?


-Mads
Back to top
madsjuul



Joined: 22 Jan 2007
Posts: 3

PostPosted: Mon Jan 22, 2007 12:53 pm    Post subject: Reply with quote

Actually Im think I want the same as this post
http://www.gnope.org/forum/viewtopic.php?t=433

I want to connect a method to a signal which is send automatically in a loop
when the application is running , something that the main loop is sending
is this possible
-mads
Back to top
tulpe



Joined: 06 Sep 2006
Posts: 7

PostPosted: Mon Jan 22, 2007 6:18 pm    Post subject: Reply with quote

use
Code:
Gtk::timeout_add(1000, 'myTimeoutFunctionName');


and a callback function

Code:
function myTimeoutFunctionName() {
  # do something...
   return TRUE; // <- important! if this is missing, the timout gets deleted
}


if you need to call a method inside an object use:
Code:
Gtk::timeout_add(1000, array($object, 'myTimeoutFunctionName'));



Documentation:
http://gtk.php.net/manual/en/gtk.method.timeout_add.php
Back to top
kksou



Joined: 06 Sep 2006
Posts: 25

PostPosted: Tue Jan 23, 2007 9:40 am    Post subject: Reply with quote

Gtk::timeout_add() allows you to call your function at fixed time interval. There's advantages and disadvantages to this depending on your application.

You may also want to take a look at Gtk::idle_add(). With this, every time php-gtk is idle, it will make a call to your function. For applications like msn messenger, we usually use Gtk::idle_add().

There is an example of Gtk::idle_add() at:
http://www.kksou.com/php-gtk2/articles/display-progress-bar-while-processing-long-task---Part-2-using_idle_add.php

Regards,
/kksou
Back to top
madsjuul



Joined: 22 Jan 2007
Posts: 3

PostPosted: Tue Jan 23, 2007 10:35 am    Post subject: Reply with quote

Thank you
This Is Great help, Its really Exiting to be able to create destop applications this easy :_)
Mads
Back to top
leon_pegg



Joined: 12 Jun 2006
Posts: 113
Location: hemel / UK

PostPosted: Wed Mar 14, 2007 5:46 pm    Post subject: Reply with quote

I know ths is a few months old now but when using sockets in php-gtk there is no need to use timeout_add or idle_add there is a special functions for working with streams the function is Gtk::io_add_watch and is used like so

$socket; // this is your stream this can be a socket
Gtk::io_add_watch($stream,Gtk::IO_IN,'callback');

function callback($stream, $conditions){
// do somthing with your stream
return true; // this is important if you dont return true the watch dies
}

ok the secon argument to Gtk::io_add_watch is the type of watch there a several things you can watch for these are as follows.

* Gtk::IO_IN : stream is ready for reading,
* Gtk::IO_OUT : stream is ready for writing,
* Gtk::IO_PRI : this is the high priority channel for stream
* Gtk::IO_ERR : stream is in error state,
* Gtk::IO_HUP : stream has been disconnected
* Gtk::IO_NVAL : (invalid command to socket ?)

hope this helps you.
Back to top
xshape



Joined: 09 Apr 2007
Posts: 1

PostPosted: Mon Apr 09, 2007 8:06 am    Post subject: To leon_pegg Reply with quote

I've used ur code and Gtk::io_add_watch for my program.its used to be a simple chat with GUI ,when i use Gtk::io_add_watch my program freeze.as soon as it gets incoming it show the message in callback and it did not respond to clients anymore and i guess ther return true; did not work properly.

this is my program.
.
.
//my GUI elements are added here
.
.
.
.
$win->add($vbox);
$win->show_all();

$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
Gtk::io_add_watch(stream_socket_accept($socket,-1),Gtk::IO_IN,'callback');

function callback($stream, $conditions){
$sock_data = fread($stream, 2048);
echo "The client has sent :";
var_dump($sock_data);
fwrite($stream, "You have sent :[".$sock_data."]\n");
fclose($stream);
return true;
}

Gtk::main();
Back to top
leon_pegg



Joined: 12 Jun 2006
Posts: 113
Location: hemel / UK

PostPosted: Mon Apr 09, 2007 6:05 pm    Post subject: Reply with quote

Hi xshape,
your problem is here

Gtk::io_add_watch(stream_socket_accept($socket,-1),Gtk::IO_IN,'callback');

replace this line with

Gtk::io_add_watch($socket,Gtk::IO_IN,'accept_callback');

and add this function

function accept_callback($socket, $conditions){
$client = stream_socket_accept($socket);
Gtk::io_add_watch($client,Gtk::IO_IN,'callback');
}

this is the very basics you need but you should keep an array of the clients connected and also connect the HUP and ERR sigs so that you can close the client connection.

hope this helps
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.