Inter-Desktop Communication"

From Documentation
m (remove empty version history (via JWB))
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
Unlike pages, you cannot access two desktops at the same time. You cannot send or post an event from one desktop to another directly, either. Rather, we have to use [[ZK Developer's Reference/Event Handling/Event Queues|a event queue]] with a proper scope, such as group, session or application -- depending on where the other desktop is located.
+
Unlike pages, you cannot access two desktops at the same time. You cannot send or post an event from one desktop to another directly either. Rather, we have to use [[ZK Developer's Reference/Event Handling/Event Queues|an event queue]] with a proper scope, such as group, session or application -- depending on where the other desktop is located.
  
 
=Desktops in the Same Browser Window=
 
=Desktops in the Same Browser Window=
  
In most cases, a browser window has at most one desktop. However, it is still possible to multiple desktops in one browser window:
+
In most cases, each browser window has at most one desktop. However, it is still possible to have multiple desktops in one browser window:
  
 
* Use HTML IFRAME or FRAMESET to integrate multiple ZUML pages
 
* Use HTML IFRAME or FRAMESET to integrate multiple ZUML pages
* Use a portal server to integrate multiple ZK portlet
+
* Use a portal server to integrate multiple ZK portlets
* Assembly multiple ZUML pages at the client, such as [[ZK Developer's Reference/Integration/Use ZK as Fragment in Foreign Templating Framework|this templating technology]]
+
* Assemble multiple ZUML pages at the client, such as the templating technology described in [[ZK Developer's Reference/Integration/Use ZK as Fragment in Foreign Templating Framework|this section]]
  
In this case, you could communicate among desktops by use of an event queue with the group scope.
+
In this case, you could communicate among desktops by the use of an event queue with the group scope (<javadoc method="GROUP">org.zkoss.zk.ui.event.EventQueues</javadoc>).
  
<source lang="java" high="1">
+
<source lang="java" highlight="1">
 
EventQueue que = EventQueues.lookup("groupTest", EventQueues.GROUP, true);
 
EventQueue que = EventQueues.lookup("groupTest", EventQueues.GROUP, true);
 
que.subscribe(new EventListener() {
 
que.subscribe(new EventListener() {
Line 21: Line 21:
 
});
 
});
 
</source>
 
</source>
 +
 +
Notice that the desktop-scoped event queue does not require [[ZK Developer's Reference/Server Push|Server Push]], so there is no performance impact at all.
  
 
Here is a dumb example: chat among iframes.
 
Here is a dumb example: chat among iframes.
  
<source lang="xml">
+
<source lang="xml" highlight="4">
<-- main -->
+
<!-- main -->
 
<window title="main" border="normal" onOK="publish()">
 
<window title="main" border="normal" onOK="publish()">
 
<zscript>
 
<zscript>
Line 42: Line 44:
 
}
 
}
 
</zscript>
 
</zscript>
Please type some words into the short textbox, and then you should see every high textbox will append the same words.
+
Please enter:
 
<textbox id="i" onChange="publish()"/>
 
<textbox id="i" onChange="publish()"/>
 
<textbox id="o" rows="6"/>
 
<textbox id="o" rows="6"/>
Line 78: Line 80:
  
 
=Desktop in Different Sessions=
 
=Desktop in Different Sessions=
=Version History=
+
 
{{LastUpdated}}
+
Similarly, we could use an event queue to communicate among desktops belonging to different sessions. The only difference is to specify <javadoc method="APPLICATION">org.zkoss.zk.ui.event.EventQueues</javadoc> as the scope.
{| border='1px' | width="100%"
+
 
! Version !! Date !! Content
+
<source lang="java">
|-
+
EventQueue que = EventQueues.lookup("groupTest", EventQueues.APPLICATION, true);
| &nbsp;
+
</source>
| &nbsp;
+
 
| &nbsp;
+
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 05:54, 6 February 2024


Inter-Desktop Communication


Unlike pages, you cannot access two desktops at the same time. You cannot send or post an event from one desktop to another directly either. Rather, we have to use an event queue with a proper scope, such as group, session or application -- depending on where the other desktop is located.

Desktops in the Same Browser Window

In most cases, each browser window has at most one desktop. However, it is still possible to have multiple desktops in one browser window:

  • Use HTML IFRAME or FRAMESET to integrate multiple ZUML pages
  • Use a portal server to integrate multiple ZK portlets
  • Assemble multiple ZUML pages at the client, such as the templating technology described in this section

In this case, you could communicate among desktops by the use of an event queue with the group scope (EventQueues.GROUP).

EventQueue que = EventQueues.lookup("groupTest", EventQueues.GROUP, true);
que.subscribe(new EventListener() {
	public void onEvent(Event evt) {
		//receive event from this event queue (within the same group of desktops)
	}
});

Notice that the desktop-scoped event queue does not require Server Push, so there is no performance impact at all.

Here is a dumb example: chat among iframes.

<!-- main -->
<window title="main" border="normal" onOK="publish()">
	<zscript>
	EventQueue que = EventQueues.lookup("groupTest", "group", true);
	que.subscribe(new EventListener() {
		public void onEvent(Event evt) {
			o.setValue(o.getValue() + evt.getData() + "\n");
		}
	});
	void publish() {
		String text = i.getValue();
		if (text.length() > 0) {
			i.setValue("");
			que.publish(new Event("onGroupTest", null, text));
		}
	}
	</zscript>
	Please enter:
	<textbox id="i" onChange="publish()"/>		
	<textbox id="o" rows="6"/>
	<separator/>
	<iframe src="includee.zul" height="500px" width="30%"/>
	<iframe src="includee.zul" height="500px" width="30%"/>
	<iframe src="includee.zul" height="500px" width="30%"/>
</window>

And, this is the ZUML page being referenced (by iframe).

<!-- includee.zul -->
<window title="frame2" border="normal" onOK="publish()">
	<zscript>
		EventQueue que = EventQueues.lookup("groupTest", "group", true);
		que.subscribe(new EventListener() {
			public void onEvent(Event evt) {
				o.setValue(o.getValue() + evt.getData() + "\n");
			}
		});
		void publish() {
			String text = i.getValue();
			if (text.length() > 0) {
				i.setValue("");
				que.publish(new Event("onGroupTest", null, text));
			}
		}
	</zscript>
	<textbox id="i" onChange="publish()"/>		
	<textbox id="o" rows="6"/>
</window>

Desktop in Different Sessions

Similarly, we could use an event queue to communicate among desktops belonging to different sessions. The only difference is to specify EventQueues.APPLICATION as the scope.

EventQueue que = EventQueues.lookup("groupTest", EventQueues.APPLICATION, true);




Last Update : 2024/02/06

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.