设为首页 收藏本站
查看: 553|回复: 0

[经验分享] Comet-Jetty(1)CometD Design and Go through document

[复制链接]

尚未签到

发表于 2017-2-27 09:35:26 | 显示全部楼层 |阅读模式
Comet-Jetty(1)CometD Design and Go through document

First of all, I tried jetty6.1.3, chat demo. I have demoed that before. But I do not think it is a good way to solve my problem on hand.

So I try to download the latest cometd2.x project and read the document.

1. Prepare the project
>git clone https://github.com/cometd/cometd.git

build the whole project at the root path
>mvn clean install

I think there are many parts useful to me.
1.1 full chat applications with jquery
1.2 examples of extensions such as message acknowledgement, reload, timesync and timestamp.
1.3 an example of how to echo private message to a particular client only

running the demos with maven.
>cd cometd-demo
>mvn jetty:deploy-war

Then I visit the URL http://localhost:8080 to take a look at the demo.

2. Primer
2.2.1 Setting Up the Project in Maven Way
CometD libraries using maven uses the maven archetype, which create the skeleton of the project.
>cd D:\work\cometd
>mvn archetype:generate -DarchetypeCatalog=http://cometd.org

I choose 5 to have a try here:
5: http://cometd.org -> org.cometd.archetypes:cometd-archetype-spring-jquery-jetty7 (2.4.3 - CometD
archetype for creating a server-side event-driven web application)

Confirm properties configuration:
groupId: com.sillycat
artifactId: easycometd
version: 1.0
package: com.sillycat.easycometd
cometdVersion: 2.4.3
jettyVersion: 7.6.4.v20120524
slf4jVersion: 1.6.4
springVersion: 3.1.1.RELEASE

I copy the directory to my working directory d:/work/easy/easycometd.
>cd d:/work/easy/easycometd
>mvn eclipse:eclipse
I can import this project to eclipse. I delete the parent configuration in pom
<parent>
    <artifactId>cometd-project</artifactId>
    <groupId>org.cometd</groupId>
    <version>2.5.0-SNAPSHOT</version>
</parent>

And take a look at the demo.
>mvn install jetty:run

We can visit this URL http://localhost:8080/easycometd/, but there is not much information. I will try more on this demo after I read documents.

3. Concept & Architecture
The CometD Project uses the Bayeux protocol to exchange the information between the client and the server.
The unit of information exchanged is a Bayeux message formatted in JSON.

All messages the client and server exchange have a channel field. The channel field provides the characterization of messages in classes. The channel is a central concept in CometD: publishers publish messages to channels, and subscribers subscribe to channels to receive messages. This is strongly reflected in the CometD APIs.

3.1.1.Channel Definitions
A channel that starts with /meta/ is a meta channel, a channel that starts with /service/ is a service channel, and all other channels are broadcast channels.

3.1.1.1 Meta Channels
CometD implementation creates meta channels, application can not. Provide information about protocol.

3.1.1.2. Service Channels
Applications create service channels for request/response style of communication.

3.1.1.3. Broadcast Channels
Applications also create broadcast channels. One sender wants to broadcast information to multiple recipients.

3.1.1.4. Use of Wildcards in Channels

3.3.1 Sessions
Client Sessions
org.cometd.bayeux.client.ClientSession
org.cometd.bayeux.client.BayeuxClient
Server Sessions
org.cometd.bayeux.server.ServerSession

they are the counterpart of client sessions. When a client creates a client session, it is not initially associated with a correspondent server session. Only when a client session establishes the Bayeux communication with the server does the server create its correspondent server session, as well as the link between the two half-objects.

Local Sessions
org.cometd.bayeux.server.LocalSession

Local sessions can be thought of as clients living in the server. They do not represent a remote client, but instead a server-side client.

3.3.4 Message Processing
3.3.5. Application Interaction

4.1. Configuring and Initializing
in application.js, we can see the configuring:
var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
       cometd.configure({
           url: cometURL,
           logLevel: 'debug'
       });
After we call cometd.handshake(); The client will connect to the server.
The call to handshake() (or to init()) initiates the Bayeux communication with the Bayeux server.

4.2. Handshaking
The client and the server negotiate the type of transport to use.
Once the transport negotiates successfully, the server informs the client with the detailed timings of the requests.

4.3.4. Subscribers and Listeners
addListener() ----> removeListener()
subscribe()    ----> unsubscribe()

addListener()
Must be used to listen to meta channel messages
May be used to listen to service channel messages
Should not be used to listen broadcast channel messages(use subscribe() instead)
Is synchronous
Does not involve any communication with the Bayeux server, and as such can be called before calling handshake().

subscribe()
Must not be used to listen to meta channels messages
May be used to listen to service channel message(but recommend, prefer to use addListener())
Should be used to listen to broadcast channel messages
Involves a communication with the Bayeux server and as such cannot be called before calling handshake().
Is asynchronous:

4.3.6. Wildcard Subscriptions
cometd.subscribe("/chatrooms/*", function(message) { ... });

4.3.7. Meta Channel List
/meta/handshake
/meta/connect
/meta/disconnect
/meta/subscribe
/meta/unsubscribe
/meta/publish
/meta/unsuccessful

4.4. Publishing
publish data onto a certain channel
cometd.publish('/mychannel',{ mydata: { foo: 'bar' } }};

It is asynchronous: it returns immediately, well before the Bayeux server has received the message.

4.7. JavaScript Transports
4.7.1. The long-polling Transport
The long-polling transport is the default transport if the browser and the server do not support WebSockets. The data is sent to the server by means of a POST request with Content-Type application/json via a plain XMLHttpRequest call.
This transport is used when the communication with the Bayeux server happens on the same domain.

Client will ask the Server for reply, if no data, Client will wait to timeout, if data, Client will receive and make another request.

4.7.2. The callback-polling Transport
The callback-polling transport is used when the communication with the Bayeux server happens on a different domain.
To overcome XMLHttpRequest restrictions, this transport uses the JSONP script injection, which injects a <script> element whose src attribute points to the Bayeux server.

4.7.3. The websocket Transport
4.7.4. Unregistering Transports
var cometd = dojox.cometd; // Dojo style
var cometd = $.cometd; // jQuery style
cometd.unregisterTransport('websocket');

The purpose is to disable certain transports that might be unreliable.


references:
http://timyang.net/category/architecture/
http://sillycat.iteye.com/blog/563598

http://wiki.eclipse.org/Jetty/
https://github.com/eclipse/jetty.project
http://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project.git

http://ftp.jaist.ac.jp/pub/eclipse/jetty/index.html

http://oss.sonatype.org/content/groups/jetty/

http://cometd.org/
http://haidii.iteye.com/blog/481004
https://github.com/cometd
https://github.com/cometd/cometd.git
http://docs.cometd.org/reference/
http://abentotoro.blog.sohu.com/203996578.html

http://yaojialing.iteye.com/blog/716094

http://docs.cometd.org/reference/#d0e117

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347738-1-1.html 上篇帖子: 天生一对"Maven2+Jetty" -- Maven2创建并管理WebApp 下篇帖子: 使用Maven编译Android下i-jetty的一些问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表