-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Please focus on this code below:
public void run() { if (!desktop.isServerPushEnabled()) { desktop.enableServerPush(true); } try { while (!_ceased) { try { Executions.activate(desktop); // I wanna get parameters from URL process();// something doing with using URL parameters } } finally { Executions.deactivate(desktop); } Threads.sleep(2000);// Update each 2 seconds } } catch (InterruptedException ex) { ex.printStackTrace(); } finally { if (desktop.isServerPushEnabled()) desktop.enableServerPush(false); } }
So how can I get the URL parameters dynamically when the program is running Server Push?
(ZK ver:3.60)
Hi,
I'm not familiar with server push mechanism,
after studying the source code of smalltalk "chatroom",
http://www.zkoss.org/smalltalks/serverpush/
I think the global variable is accessed by the method "process()",
that is,
maybe you can make your URL with this way,
thus every client can access the same parameters.
This is just personal opinion, I haven't made this idea come true....
to get parameter, make sure you are in the http request or zk event thread (not in your thread)
I think you should save the parameter that you want in some where(such as desktop) when page loading (ex, in onCreate).
And, then get it back in your control.
I know, I can get parameter from desktop like "onCreate" but I wanna get the parameter "dynamically". It means the URL may be changed because other program may post to my program. So I need to get parameter when it's going to do server push.
place the parameter in desktop doesn't work here...
how can I do this? thanks.
I think I have some miss understanding, could you provide some example code to show what you want to do.
for example, now I have a program A that it will post URL to program B(using server push to get parameters from URL). Program A acts the post URL action when user clicks the button(program A will construct the data and post the URL to program B after button event has started). Then program B should get the parameter either URL have changed or not(changed by program A).
now the problem is, the parameter return null doing in the Thread Class. I need to get parameter when program B enables server push.
I don't understand the "program A will post a URL to program B".
what does the 'program' mean in this case? a desktop with a button?
what does 'post a URL to B ' mean? change url of another desktop?
if you have the code, please do some filter(simplify) and post it here.
don't let us guess the code.
I'm sorry that I have made a confusing of what is "program A and program B".
now I have two ZK web dynamic project(call it prj1 and prj2).
prj1 has a combobox. It posts URL when user selects the combobox.
here is a piece of prj1's index.uzl code
<combobox id="foodNameCombo" readonly="true"> <zscript> String sessionKey = Executions.getCurrent().getParameter("session_key"); </zscript> <attribute name="onSelect"><![CDATA[ org.web3.grapes.pojo.FoodInfo selectedFoodInfo = self.getSelectedItem().getValue(); org.web3.grapes.Message msg = new org.web3.grapes.Message(); msg.type = "org.web3.imhungry?food_data"; msg.setData("food_name", selectedFoodInfo.getFoodName()); msg.setData("restaurant_name", selectedFoodInfo.getRestaurantName()); msg.setData("address", selectedFoodInfo.getAddress()); msg.setData("description", selectedFoodInfo.getDescription()); msg.setData("rating", selectedFoodInfo.getRating()); msg.setData("flickr_text", selectedFoodInfo.getFlickrText()); String appId = "org.web3.imhungry"; System.out.println(sessionKey); String secret = "passw0rd"; String serverUrl = "http://xx.xx.xx.xx"; org.grapes.wine.api.client.rest.GrapesRestClient client = new org.grapes.wine.api.client.rest.GrapesRestClient(appId, sessionKey, secret, serverUrl); client.forwardMessage("org.web3.gmap", msg); ]]></attribute> <comboitem forEach="${foodInfo}" label="${each.foodName}" value="${each}" /> </combobox>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false &key=${desktopScope.gmapsKey}" unless="${empty desktopScope.gmapsKey}" type="text/javascript"> </script> <gmaps id="food_map" width="500px" height="320px" showSmallCtrl="true" lat="39.155622393423215" lng="116.927490234375" zoom="10" enableGoogleBar="true" showTypeCtrl="true" use="org.web3.imhungry.map.FoodMap"> <gmarker id="centerMarker" lat="39.155622393423215" lng="116.927490234375" iconImage="http://www.google.com/mapfiles/markerZ.png"> </gmarker> <attribute name="onMapClick"><![CDATA[ org.zkoss.gmaps.Gmarker gmarker = event.getGmarker(); if (gmarker != null) { gmarker.setOpen(true); } ]]></attribute>
public void init() { desktop = Executions.getCurrent().getDesktop(); desktop.setAttribute("gmapsKey", gKey); Execution execution = Executions.getCurrent(); String sessionKey = execution.getParameter("session_key"); System.out.println("start: "+sessionKey); foodMap = this; } public void onCreate() { if (!foodMap.getChildren().isEmpty()) { Gmarker marker = (Gmarker) foodMap.getChildren().get(0); marker.setContent("<img src='images/food.png'/>"); marker.setOpen(true); } desktop.enableServerPush(true); GmapUpdate gmapUpdate = new GmapUpdate(desktop); gmapUpdate.start(); }
public void run() { if (!desktop.isServerPushEnabled()) { desktop.enableServerPush(true); } try { while (!_ceased) { try { Executions.activate(desktop); System.out.println(Executions.getCurrent().getParameter("session_key")); System.out.println("xml: "+Executions.getCurrent().getParameter("xml")); System.out.println("sk: "+desktop.getExecution().getParameter("session_key")); // Executions.getCurrent().getParameter always return null! if (canRun()) { process(); } } finally { Executions.deactivate(desktop); } Threads.sleep(2000);// Update each 2 seconds } } catch (InterruptedException ex) { ex.printStackTrace(); } finally { if (desktop.isServerPushEnabled()) desktop.enableServerPush(false); } }
I still don't know the "project1 post a form to project2" mean. is this post trigger on server side back-end? not in client ?
let me make sure some scenario,
1. a user open a browser connect to page (has a combo box) in site 1 (project 1)
Client -> Site A
2. user select a value combobox, in server side onSelect, it (send)post a form to another page in another site.
Client -> Site A
3. in this send/post handler , it do some server push(I think it is server to server message, not relate to client)
Site A -> Site B (GMap page)
I think you are confused with ZK/Web App life cycle and concept and scope,
or I am confused with your case.
thanks for your reply.
the step one and two are correct.
for step three, the site B(Gmap page) will receive the URL(do server push for updating Gmap). the client in site B will check the URL(actually, the URL has some parameter(like session_key) to specify which client can receive it). If the session_key and "message name"(provided by my API) is match, a client in site B can do process.
Here is a canRun() method code in GmapUpdate class.
public boolean canRun() { boolean runProcess = true; String msgXml = //the message should be got from the newest parameter //(String) desktop.getAttribute("xml"); if (msgXml != null) { Message msg = MessageUtils.getMessageFromXML(msgXml); if (msg.type.equals("org.web3.imhungry?food_data")) { foodInfo.setAddress((String) msg.getData("address")); foodInfo.setDescription((String) msg.getData("description")); foodInfo.setFlickrText((String) msg.getData("flickr_text")); foodInfo.setFoodName((String) msg.getData("food_name")); foodInfo.setRating(Integer.parseInt((String) msg .getData("rating"))); foodInfo.setRestaurantName((String) msg .getData("restaurant_name")); } } else { runProcess = false; } return runProcess; }
do you mean
User-> Client(A) ---------------=> Site A --> a Message Post Process | (A post a http request to Sib) | User-> Client(B) <--server push--> Site B <-- a Thread receive message and update desktop
Asked: 2009-04-22 07:54:14 +0800
Seen: 1,162 times
Last updated: Apr 24 '09