- Added sources for stock-comet-jquery sample
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@979748 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
06d8b66db0
commit
016a688432
13 changed files with 7263 additions and 0 deletions
85
sca-java-2.x/contrib/samples/stock-comet-jquery/pom.xml
Normal file
85
sca-java-2.x/contrib/samples/stock-comet-jquery/pom.xml
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.apache.tuscany.sca</groupId>
|
||||
<artifactId>tuscany-sca</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<groupId>org.apache.tuscany.sca</groupId>
|
||||
<artifactId>sample-stock-comet-jquery-webapp</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.0</version>
|
||||
<name>stock-comet-jquery</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.atmosphere</groupId>
|
||||
<artifactId>atmosphere-jersey</artifactId>
|
||||
<version>0.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.atmosphere</groupId>
|
||||
<artifactId>atmosphere-runtime</artifactId>
|
||||
<version>0.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.servlet</artifactId>
|
||||
<version>3.0-b73</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-json</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-websocket</artifactId>
|
||||
<version>7.1.1.v20100517</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>jetty</artifactId>
|
||||
<version>6.1.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>jetty-util</artifactId>
|
||||
<version>6.1.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>jetty-management</artifactId>
|
||||
<version>6.1.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ning</groupId>
|
||||
<artifactId>async-http-client</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tuscany.sca.shades</groupId>
|
||||
<artifactId>tuscany-base-nodep</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>stock-comet-jquery</finalName>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package org.apache.tuscany.sample.comet;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
|
||||
import org.atmosphere.annotation.Broadcast;
|
||||
import org.atmosphere.cpr.Broadcaster;
|
||||
import org.atmosphere.cpr.DefaultBroadcaster;
|
||||
import org.atmosphere.jersey.Broadcastable;
|
||||
import org.atmosphere.jersey.SuspendResponse;
|
||||
import org.oasisopen.sca.ComponentContext;
|
||||
import org.oasisopen.sca.annotation.Context;
|
||||
import org.oasisopen.sca.annotation.Reference;
|
||||
|
||||
import com.sun.jersey.spi.container.servlet.PerSession;
|
||||
|
||||
@Path("/tuscany-comet")
|
||||
@Produces("text/html;charset=ISO-8859-1")
|
||||
@PerSession
|
||||
public class CometBinding {
|
||||
|
||||
// TODO: fix Tuscany injection
|
||||
@Reference
|
||||
public StockService service;
|
||||
|
||||
@Context
|
||||
public ComponentContext context;
|
||||
|
||||
private Broadcaster broadcaster = new DefaultBroadcaster();
|
||||
|
||||
@GET
|
||||
public SuspendResponse<String> register() {
|
||||
System.out.println("Service reference: " + service);
|
||||
System.out.println("Context reference: " + context);
|
||||
return new SuspendResponse.SuspendResponseBuilder<String>()
|
||||
.broadcaster(broadcaster)
|
||||
.outputComments(true)
|
||||
.addListener(new EventsLogger())
|
||||
.build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Broadcast
|
||||
public Broadcastable publish() {
|
||||
System.out.println("Service reference: " + service);
|
||||
System.out.println("Context reference: " + context);
|
||||
return new Broadcastable("ASF#" + new Random(new Date().getTime()).nextInt(1000), "", broadcaster);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
||||
*
|
||||
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* The contents of this file are subject to the terms of either the GNU
|
||||
* General Public License Version 2 only ("GPL") or the Common Development
|
||||
* and Distribution License("CDDL") (collectively, the "License"). You
|
||||
* may not use this file except in compliance with the License. You can obtain
|
||||
* a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
|
||||
* or jersey/legal/LICENSE.txt. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
* When distributing the software, include this License Header Notice in each
|
||||
* file and include the License file at jersey/legal/LICENSE.txt.
|
||||
* Sun designates this particular file as subject to the "Classpath" exception
|
||||
* as provided by Sun in the GPL Version 2 section of the License file that
|
||||
* accompanied this code. If applicable, add the following below the License
|
||||
* Header, with the fields enclosed by brackets [] replaced by your own
|
||||
* identifying information: "Portions Copyrighted [year]
|
||||
* [name of copyright owner]"
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* If you wish your version of this file to be governed by only the CDDL or
|
||||
* only the GPL Version 2, indicate your decision by adding "[Contributor]
|
||||
* elects to include this software in this distribution under the [CDDL or GPL
|
||||
* Version 2] license." If you don't indicate a single choice of license, a
|
||||
* recipient has the option to distribute your version of this file under
|
||||
* either the CDDL, the GPL Version 2 or to extend the choice of license to
|
||||
* its licensees as provided above. However, if you add GPL Version 2 code
|
||||
* and therefore, elected the GPL Version 2 license, then the option applies
|
||||
* only if the new code is made subject to such option by the copyright
|
||||
* holder.
|
||||
*/
|
||||
package org.apache.tuscany.sample.comet;
|
||||
|
||||
import org.atmosphere.cpr.AtmosphereResourceEvent;
|
||||
import org.atmosphere.cpr.AtmosphereResourceEventListener;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class EventsLogger implements AtmosphereResourceEventListener {
|
||||
|
||||
public EventsLogger() {
|
||||
}
|
||||
|
||||
public void onSuspend(final AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
|
||||
System.out.println("onSuspend: " + event.getResource().getRequest().getRemoteAddr()
|
||||
+ event.getResource().getRequest().getRemotePort());
|
||||
}
|
||||
|
||||
public void onResume(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
|
||||
System.out.println("onResume: " + event.getResource().getRequest().getRemoteAddr());
|
||||
}
|
||||
|
||||
public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
|
||||
System.out.println("onDisconnect: " + event.getResource().getRequest().getRemoteAddr()
|
||||
+ event.getResource().getRequest().getRemotePort());
|
||||
}
|
||||
|
||||
public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
|
||||
System.out.println("onBroadcast: " + event.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
||||
*
|
||||
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* The contents of this file are subject to the terms of either the GNU
|
||||
* General Public License Version 2 only ("GPL") or the Common Development
|
||||
* and Distribution License("CDDL") (collectively, the "License"). You
|
||||
* may not use this file except in compliance with the License. You can obtain
|
||||
* a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
|
||||
* or jersey/legal/LICENSE.txt. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
* When distributing the software, include this License Header Notice in each
|
||||
* file and include the License file at jersey/legal/LICENSE.txt.
|
||||
* Sun designates this particular file as subject to the "Classpath" exception
|
||||
* as provided by Sun in the GPL Version 2 section of the License file that
|
||||
* accompanied this code. If applicable, add the following below the License
|
||||
* Header, with the fields enclosed by brackets [] replaced by your own
|
||||
* identifying information: "Portions Copyrighted [year]
|
||||
* [name of copyright owner]"
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* If you wish your version of this file to be governed by only the CDDL or
|
||||
* only the GPL Version 2, indicate your decision by adding "[Contributor]
|
||||
* elects to include this software in this distribution under the [CDDL or GPL
|
||||
* Version 2] license." If you don't indicate a single choice of license, a
|
||||
* recipient has the option to distribute your version of this file under
|
||||
* either the CDDL, the GPL Version 2 or to extend the choice of license to
|
||||
* its licensees as provided above. However, if you add GPL Version 2 code
|
||||
* and therefore, elected the GPL Version 2 license, then the option applies
|
||||
* only if the new code is made subject to such option by the copyright
|
||||
* holder.
|
||||
*/
|
||||
package org.apache.tuscany.sample.comet;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.PathSegment;
|
||||
|
||||
@Path("/")
|
||||
@Produces("text/html")
|
||||
public class FileResource {
|
||||
|
||||
@Context
|
||||
private ServletContext sc;
|
||||
|
||||
@Path("/js/{id}")
|
||||
@GET
|
||||
public InputStream getJQuery(@PathParam("id") PathSegment ps) {
|
||||
return sc.getResourceAsStream("/js/" + ps.getPath());
|
||||
}
|
||||
|
||||
@GET
|
||||
public InputStream getIndex() {
|
||||
return sc.getResourceAsStream("/index.html");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.tuscany.sample.comet;
|
||||
|
||||
public interface StockService {
|
||||
|
||||
String getSymbol();
|
||||
|
||||
Double getValue();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.tuscany.sample.comet;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
public class StockServiceImpl implements StockService {
|
||||
|
||||
public static final int MAX_VALUE = 1000;
|
||||
private Random random = new Random(new Date().getTime());
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return "ASF";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getValue() {
|
||||
Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE));
|
||||
return Double.valueOf(new DecimalFormat("#.##").format(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Context>
|
||||
<!-- <Loader className="org.atmosphere.util.AtmosphereClassloader"/> -->
|
||||
<Loader delegate="true"/>
|
||||
</Context>
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
-->
|
||||
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
|
||||
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
|
||||
targetNamespace="http://samples"
|
||||
name="Stock">
|
||||
|
||||
<component name="AtmosphereServlet">
|
||||
<implementation.web web-uri=""/>
|
||||
<reference name="service" target="StockService"/>
|
||||
</component>
|
||||
|
||||
<component name="StockService">
|
||||
<implementation.java class="org.apache.tuscany.sample.comet.StockServiceImpl"/>
|
||||
</component>
|
||||
|
||||
</composite>
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:j2ee="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3.0.xsd">
|
||||
<description>Apache Tuscany Stock Comet jQuery Sample</description>
|
||||
<display-name>Apache Tuscany Stock Comet jQuery Sample</display-name>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<servlet>
|
||||
<description>AtmosphereServlet</description>
|
||||
<servlet-name>AtmosphereServlet</servlet-name>
|
||||
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
|
||||
<async-supported>true</async-supported>
|
||||
<load-on-startup>0</load-on-startup>
|
||||
<init-param>
|
||||
<param-name>com.sun.jersey.config.property.packages</param-name>
|
||||
<param-value>org.apache.tuscany.sample.comet</param-value>
|
||||
</init-param>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>AtmosphereServlet</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
||||
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
<!--
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<title>Apache Tuscany Asynchronous Servlet Sample</title>
|
||||
<script type="text/javascript" src="stock-comet-jquery/js/jquery-1.4.2.js"></script>
|
||||
<script type="text/javascript" src="stock-comet-jquery/js/jquery.atmosphere.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function()
|
||||
{
|
||||
var connectedEndpoint;
|
||||
|
||||
function update(response) {
|
||||
var data = response.responseBody;
|
||||
var aux = data.toString().split('#');
|
||||
document.getElementById('symbol').textContent = aux[0];
|
||||
document.getElementById('price').textContent = aux[1];
|
||||
}
|
||||
|
||||
document.getElementById('connect').onclick = function(event) {
|
||||
/* transport can be : long-polling, streaming or websocket */
|
||||
$.atmosphere.subscribe(document.location.toString() + '/tuscany-comet',
|
||||
update,
|
||||
$.atmosphere.request = {transport: document.getElementById('transport').value});
|
||||
connectedEndpoint = $.atmosphere.response;
|
||||
|
||||
document.getElementById('connect').disabled = true;
|
||||
document.getElementById('transport').disabled = true;
|
||||
document.getElementById('get-values').disabled = false;
|
||||
}
|
||||
|
||||
document.getElementById('get-values').disabled = true;
|
||||
document.getElementById('get-values').onclick = function(event) {
|
||||
connectedEndpoint.push(document.location.toString() + '/tuscany-comet',
|
||||
null,
|
||||
$.atmosphere.request = {method: 'POST'});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Apache Tuscany Asynchronous Servlet Sample</h2>
|
||||
<label>Select transport</label>
|
||||
<select id="transport">
|
||||
<option id="streaming" value="streaming">http streaming</option>
|
||||
<option id="long-polling" value="long-polling">long-polling</option>
|
||||
<option id="websocket" value="websocket">websocket</option>
|
||||
</select>
|
||||
<input id='connect' type='submit' value='Connect'/>
|
||||
<h3>Stock Monitor</h3>
|
||||
<input id='get-values' type="submit" value='Get values'/>
|
||||
<p/>
|
||||
<div id='text'>
|
||||
<b><label>Company Symbol: </label></b>
|
||||
<span id="symbol">N/A</span>
|
||||
<b><label>Price: </label></b>
|
||||
<span id="price">N/A</span>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
6240
sca-java-2.x/contrib/samples/stock-comet-jquery/src/main/webapp/js/jquery-1.4.2.js
vendored
Normal file
6240
sca-java-2.x/contrib/samples/stock-comet-jquery/src/main/webapp/js/jquery-1.4.2.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,534 @@
|
|||
/**
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
jQuery.atmosphere = function()
|
||||
{
|
||||
var activeRequest;
|
||||
$(window).unload(function()
|
||||
{
|
||||
if (activeRequest)
|
||||
activeRequest.abort();
|
||||
});
|
||||
|
||||
return {
|
||||
version : 0.7,
|
||||
response : {
|
||||
status: 200,
|
||||
responseBody : '',
|
||||
headers : [],
|
||||
state : "messageReceived",
|
||||
transport : "polling",
|
||||
push : [],
|
||||
error: null,
|
||||
id : 0
|
||||
},
|
||||
|
||||
request : {},
|
||||
logLevel : 'info',
|
||||
callbacks: [],
|
||||
activeTransport : null,
|
||||
websocket : null,
|
||||
killHiddenIFrame : null,
|
||||
|
||||
subscribe: function(url, callback, request)
|
||||
{
|
||||
jQuery.atmosphere.request = jQuery.extend({
|
||||
timeout: 300000,
|
||||
method: 'GET',
|
||||
headers: {},
|
||||
contentType : "text/html;charset=ISO-8859-1",
|
||||
cache: true,
|
||||
async: true,
|
||||
ifModified: false,
|
||||
callback: null,
|
||||
dataType: '',
|
||||
url : url,
|
||||
data : '',
|
||||
suspend : true,
|
||||
maxRequest : 60,
|
||||
lastIndex : 0,
|
||||
logLevel : 'info',
|
||||
requestCount : 0,
|
||||
fallbackTransport : 'streaming',
|
||||
transport : 'long-polling'
|
||||
|
||||
}, request);
|
||||
|
||||
logLevel = jQuery.atmosphere.request.logLevel || 'info';
|
||||
if (callback != null) {
|
||||
jQuery.atmosphere.addCallback(callback);
|
||||
jQuery.atmosphere.request.callback = callback;
|
||||
}
|
||||
|
||||
if (jQuery.atmosphere.request.transport != jQuery.atmosphere.activeTransport) {
|
||||
jQuery.atmosphere.closeSuspendedConnection();
|
||||
}
|
||||
jQuery.atmosphere.activeTransport = jQuery.atmosphere.request.transport;
|
||||
|
||||
if (jQuery.atmosphere.request.transport != 'websocket') {
|
||||
jQuery.atmosphere.executeRequest();
|
||||
} else if (jQuery.atmosphere.request.transport == 'websocket') {
|
||||
if (!window.WebSocket) {
|
||||
jQuery.atmosphere.log(logLevel, ["Websocket is not supported, using request.fallbackTransport"]);
|
||||
jQuery.atmosphere.request.transport = jQuery.atmosphere.request.fallbackTransport;
|
||||
jQuery.atmosphere.executeRequest();
|
||||
}
|
||||
else {
|
||||
jQuery.atmosphere.executeWebSocket();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Always make sure one transport is used, not two at the same time except for Websocket.
|
||||
*/
|
||||
closeSuspendedConnection : function () {
|
||||
if (activeRequest != null) {
|
||||
activeRequest.abort();
|
||||
}
|
||||
|
||||
if (jQuery.atmosphere.websocket != null) {
|
||||
jQuery.atmosphere.websocket.close();
|
||||
jQuery.atmosphere.websocket = null;
|
||||
}
|
||||
},
|
||||
|
||||
executeRequest: function()
|
||||
{
|
||||
|
||||
if (jQuery.atmosphere.request.transport == 'streaming') {
|
||||
if ($.browser.msie) {
|
||||
jQuery.atmosphere.ieStreaming();
|
||||
return;
|
||||
} else if ((typeof window.addEventStream) == 'function') {
|
||||
jQuery.atmosphere.operaStreaming();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (jQuery.atmosphere.request.requestCount++ < jQuery.atmosphere.request.maxRequest) {
|
||||
jQuery.atmosphere.response.push = function (url)
|
||||
{
|
||||
jQuery.atmosphere.request.callback = null;
|
||||
jQuery.atmosphere.publish(url, null, jQuery.atmosphere.request);
|
||||
};
|
||||
|
||||
var request = jQuery.atmosphere.request;
|
||||
var response = jQuery.atmosphere.response;
|
||||
if (request.transport != 'polling') {
|
||||
response.transport = request.transport;
|
||||
}
|
||||
|
||||
var ajaxRequest;
|
||||
var error = false;
|
||||
if ($.browser.msie) {
|
||||
var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
|
||||
for (var i = 0; i < activexmodes.length; i++) {
|
||||
try {
|
||||
ajaxRequest = new ActiveXObject(activexmodes[i])
|
||||
}
|
||||
catch(e) {
|
||||
}
|
||||
}
|
||||
} else if (window.XMLHttpRequest) {
|
||||
ajaxRequest = new XMLHttpRequest();
|
||||
}
|
||||
|
||||
if (request.suspend) {
|
||||
activeRequest = ajaxRequest;
|
||||
}
|
||||
|
||||
ajaxRequest.open(request.method, request.url, true);
|
||||
ajaxRequest.setRequestHeader("X-Atmosphere-Framework", jQuery.atmosphere.version);
|
||||
ajaxRequest.setRequestHeader("X-Atmosphere-Transport", request.transport);
|
||||
ajaxRequest.setRequestHeader("X-Cache-Date", new Date());
|
||||
|
||||
if (!$.browser.msie) {
|
||||
ajaxRequest.onerror = function()
|
||||
{
|
||||
error = true;
|
||||
try {
|
||||
response.status = XMLHttpRequest.status;
|
||||
}
|
||||
catch(e) {
|
||||
response.status = 404;
|
||||
}
|
||||
|
||||
response.state = "error";
|
||||
jQuery.atmosphere.invokeCallback(response);
|
||||
ajaxRequest.abort();
|
||||
activeRequest = null;
|
||||
}
|
||||
}
|
||||
|
||||
ajaxRequest.onreadystatechange = function()
|
||||
{
|
||||
var junkForWebkit = false;
|
||||
var update = false;
|
||||
if (ajaxRequest.readyState == 4) {
|
||||
jQuery.atmosphere.request = request;
|
||||
if (request.suspend && ajaxRequest.status == 200) {
|
||||
jQuery.atmosphere.executeRequest();
|
||||
}
|
||||
|
||||
if ($.browser.msie) {
|
||||
update = true;
|
||||
}
|
||||
} else if (!$.browser.msie && ajaxRequest.readyState == 3 && ajaxRequest.status == 200) {
|
||||
update = true;
|
||||
} else {
|
||||
clearTimeout(request.id);
|
||||
}
|
||||
|
||||
if (update) {
|
||||
if (request.transport == 'streaming') {
|
||||
response.responseBody = ajaxRequest.responseText.substring(request.lastIndex, ajaxRequest.responseText.length);
|
||||
request.lastIndex = ajaxRequest.responseText.length;
|
||||
|
||||
if (response.responseBody.indexOf("<!--") != -1) {
|
||||
junkForWebkit = true;
|
||||
}
|
||||
|
||||
} else {
|
||||
response.responseBody = ajaxRequest.responseText;
|
||||
}
|
||||
|
||||
if (response.responseBody.indexOf("parent.callback") != -1) {
|
||||
var start = response.responseBody.indexOf("('") + 2;
|
||||
var end = response.responseBody.indexOf("')");
|
||||
response.responseBody = response.responseBody.substring(start, end);
|
||||
}
|
||||
|
||||
if (junkForWebkit) return;
|
||||
|
||||
try {
|
||||
response.status = ajaxRequest.status;
|
||||
response.headers = ajaxRequest.getAllResponseHeaders();
|
||||
}
|
||||
catch(e) {
|
||||
response.status = 404;
|
||||
}
|
||||
|
||||
if (request.suspend) {
|
||||
response.state = "messageReceived";
|
||||
} else {
|
||||
response.state = "messagePublished";
|
||||
}
|
||||
jQuery.atmosphere.invokeCallback(response);
|
||||
}
|
||||
}
|
||||
ajaxRequest.send(request.data);
|
||||
|
||||
if (request.suspend) {
|
||||
request.id = setTimeout(function()
|
||||
{
|
||||
ajaxRequest.abort();
|
||||
jQuery.atmosphere.subscribe(request.url, null, request);
|
||||
|
||||
}, request.timeout);
|
||||
}
|
||||
} else {
|
||||
jQuery.atmosphere.log(logLevel, ["Max re-connection reached."]);
|
||||
}
|
||||
},
|
||||
|
||||
operaStreaming: function()
|
||||
{
|
||||
|
||||
var url = jQuery.atmosphere.request.url;
|
||||
var es = document.createElement('event-source');
|
||||
var response = jQuery.atmosphere.response;
|
||||
|
||||
jQuery.atmosphere.response.push = function (url)
|
||||
{
|
||||
jQuery.atmosphere.request.transport = 'polling';
|
||||
jQuery.atmosphere.request.callback = null;
|
||||
jQuery.atmosphere.publish(url, null, jQuery.atmosphere.request);
|
||||
};
|
||||
|
||||
es.setAttribute('src', url);
|
||||
// without this check opera 9.5 would make two connections.
|
||||
if (opera.version() < 9.5) {
|
||||
document.body.appendChild(es);
|
||||
}
|
||||
|
||||
var operaCallback = function (event) {
|
||||
if (event.data) {
|
||||
var junkForWebkit = false;
|
||||
|
||||
response.responseBody = event.data;
|
||||
if (event.data.indexOf("<!--") != -1) {
|
||||
junkForWebkit = true;
|
||||
}
|
||||
|
||||
if (response.responseBody.indexOf("parent.callback") != -1) {
|
||||
var start = response.responseBody.indexOf("('") + 2;
|
||||
var end = response.responseBody.indexOf("')");
|
||||
response.responseBody = response.responseBody.substring(start, end);
|
||||
}
|
||||
|
||||
if (junkForWebkit) return;
|
||||
|
||||
response.state = "messageReceived";
|
||||
jQuery.atmosphere.invokeCallback(response);
|
||||
}
|
||||
};
|
||||
|
||||
es.addEventListener('payload', operaCallback, false);
|
||||
|
||||
},
|
||||
|
||||
ieStreaming : function()
|
||||
{
|
||||
var url = jQuery.atmosphere.request.url;
|
||||
jQuery.atmosphere.response.push = function (url)
|
||||
{
|
||||
jQuery.atmosphere.request.transport = 'polling';
|
||||
jQuery.atmosphere.request.callback = null;
|
||||
jQuery.atmosphere.publish(url, null, jQuery.atmosphere.request);
|
||||
};
|
||||
|
||||
transferDoc = new ActiveXObject("htmlfile");
|
||||
transferDoc.open();
|
||||
transferDoc.close();
|
||||
var ifrDiv = transferDoc.createElement("div");
|
||||
transferDoc.body.appendChild(ifrDiv);
|
||||
ifrDiv.innerHTML = "<iframe src='" + url + "'></iframe>";
|
||||
transferDoc.parentWindow.callback = jQuery.atmosphere.streamingCallback;
|
||||
}
|
||||
,
|
||||
|
||||
streamingCallback : function(args)
|
||||
{
|
||||
var response = jQuery.atmosphere.response;
|
||||
response.transport = "streaming";
|
||||
response.status = 200;
|
||||
response.responseBody = args;
|
||||
response.state = "messageReceived";
|
||||
|
||||
jQuery.atmosphere.invokeCallback(response);
|
||||
}
|
||||
,
|
||||
|
||||
executeWebSocket : function()
|
||||
{
|
||||
var request = jQuery.atmosphere.request;
|
||||
jQuery.atmosphere.log(logLevel, ["Invoking executeWebSocket"]);
|
||||
jQuery.atmosphere.response.transport = "websocket";
|
||||
var url = jQuery.atmosphere.request.url;
|
||||
var callback = jQuery.atmosphere.request.callback;
|
||||
var location = url.replace('http:', 'ws:').replace('https:', 'wss:');
|
||||
|
||||
var websocket = new WebSocket(location);
|
||||
jQuery.atmosphere.websocket = websocket;
|
||||
|
||||
jQuery.atmosphere.response.push = function (url)
|
||||
{
|
||||
var data;
|
||||
var ws = jQuery.atmosphere.websocket;
|
||||
try {
|
||||
data = jQuery.atmosphere.request.data;
|
||||
ws.send(jQuery.atmosphere.request.data);
|
||||
} catch (e) {
|
||||
jQuery.atmosphere.log(logLevel, ["Websocket failed. Downgrading to Comet and resending " + data]);
|
||||
// Websocket is not supported, reconnect using the fallback transport.
|
||||
request.transport = request.fallbackTransport;
|
||||
jQuery.atmosphere.request = request;
|
||||
jQuery.atmosphere.executeRequest();
|
||||
|
||||
// Repost the data.
|
||||
jQuery.atmosphere.request.suspend = false;
|
||||
jQuery.atmosphere.request.method = 'POST';
|
||||
jQuery.atmosphere.request.data = data;
|
||||
jQuery.atmosphere.response.state = 'messageReceived';
|
||||
jQuery.atmosphere.response.transport = request.fallbackTransport;
|
||||
jQuery.atmosphere.publish(url, null, jQuery.atmosphere.request);
|
||||
|
||||
ws.onclose = function(message) {
|
||||
}
|
||||
ws.close();
|
||||
}
|
||||
};
|
||||
|
||||
websocket.onopen = function(message)
|
||||
{
|
||||
jQuery.atmosphere.response.state = 'openning';
|
||||
jQuery.atmosphere.invokeCallback(jQuery.atmosphere.response);
|
||||
};
|
||||
|
||||
websocket.onmessage = function(message)
|
||||
{
|
||||
var data = message.data;
|
||||
if (data.indexOf("parent.callback") != -1) {
|
||||
var start = data.indexOf("('") + 2;
|
||||
var end = data.indexOf("')");
|
||||
jQuery.atmosphere.response.responseBody = data.substring(start, end);
|
||||
}
|
||||
else {
|
||||
jQuery.atmosphere.response.responseBody = data;
|
||||
}
|
||||
jQuery.atmosphere.invokeCallback(jQuery.atmosphere.response);
|
||||
};
|
||||
|
||||
websocket.onerror = function(message)
|
||||
{
|
||||
jQuery.atmosphere.response.state = 'error';
|
||||
jQuery.atmosphere.invokeCallback(jQuery.atmosphere.response);
|
||||
};
|
||||
|
||||
websocket.onclose = function(message)
|
||||
{
|
||||
jQuery.atmosphere.response.state = 'closed';
|
||||
jQuery.atmosphere.invokeCallback(jQuery.atmosphere.response);
|
||||
};
|
||||
}
|
||||
,
|
||||
|
||||
addCallback: function(func)
|
||||
{
|
||||
if (jQuery.inArray(func, jQuery.atmosphere.callbacks) == -1) {
|
||||
jQuery.atmosphere.callbacks.push(func);
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
removeCallback: function(func)
|
||||
{
|
||||
if (jQuery.inArray(func, jQuery.atmosphere.callbacks) != -1) {
|
||||
jQuery.atmosphere.callbacks.splice(index);
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
invokeCallback: function(response)
|
||||
{
|
||||
var call = function (index, func)
|
||||
{
|
||||
func(response);
|
||||
};
|
||||
|
||||
jQuery.atmosphere.log(logLevel, ["Invoking " + jQuery.atmosphere.callbacks.length + " callbacks"]);
|
||||
if (jQuery.atmosphere.callbacks.length > 0) {
|
||||
jQuery.each(jQuery.atmosphere.callbacks, call);
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
publish: function(url, callback, request)
|
||||
{
|
||||
jQuery.atmosphere.request = jQuery.extend({
|
||||
connected: false,
|
||||
timeout: 60000,
|
||||
method: 'POST',
|
||||
headers: {},
|
||||
cache: true,
|
||||
async: true,
|
||||
ifModified: false,
|
||||
callback: null,
|
||||
dataType: '',
|
||||
url : url,
|
||||
data : '',
|
||||
suspend : false,
|
||||
maxRequest : 60,
|
||||
logLevel : 'info',
|
||||
requestCount : 0,
|
||||
transport: 'polling'
|
||||
}, request);
|
||||
|
||||
if (callback != null) {
|
||||
jQuery.atmosphere.addCallback(callback);
|
||||
}
|
||||
jQuery.atmosphere.request.transport = 'polling';
|
||||
if (jQuery.atmosphere.request.transport != 'websocket') {
|
||||
jQuery.atmosphere.executeRequest();
|
||||
} else if (jQuery.atmosphere.request.transport == 'websocket') {
|
||||
if (!window.WebSocket) {
|
||||
alert("WebSocket not supported by this browser");
|
||||
}
|
||||
else {
|
||||
jQuery.atmosphere.executeWebSocket();
|
||||
}
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
unload: function (arg) {
|
||||
if (window.addEventListener) {
|
||||
document.addEventListener('unload', arg, false);
|
||||
window.addEventListener('unload', arg, false);
|
||||
} else { // IE
|
||||
document.attachEvent('onunload', arg);
|
||||
window.attachEvent('onunload', arg);
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
kill_load_bar : function() {
|
||||
if (jQuery.atmosphere.killHiddenIFrame == null) {
|
||||
jQuery.atmosphere.killHiddenIFrame = document.createElement('iframe');
|
||||
var ifr = jQuery.atmosphere.killHiddenIFrame;
|
||||
ifr.style.display = 'block';
|
||||
ifr.style.width = '0';
|
||||
ifr.style.height = '0';
|
||||
ifr.style.border = '0';
|
||||
ifr.style.margin = '0';
|
||||
ifr.style.padding = '0';
|
||||
ifr.style.overflow = 'hidden';
|
||||
ifr.style.visibility = 'hidden';
|
||||
}
|
||||
document.body.appendChild(ifr);
|
||||
ifr.src = 'about:blank';
|
||||
document.body.removeChild(ifr);
|
||||
}
|
||||
,
|
||||
|
||||
log: function (level, args)
|
||||
{
|
||||
if (window.console)
|
||||
{
|
||||
var logger = window.console[level];
|
||||
if (typeof logger == 'function')
|
||||
{
|
||||
logger.apply(window.console, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
warn: function()
|
||||
{
|
||||
log('warn', arguments);
|
||||
}
|
||||
,
|
||||
|
||||
|
||||
info :function()
|
||||
{
|
||||
if (logLevel != 'warn')
|
||||
{
|
||||
log('info', arguments);
|
||||
}
|
||||
}
|
||||
,
|
||||
|
||||
debug: function()
|
||||
{
|
||||
if (logLevel == 'debug')
|
||||
{
|
||||
log('debug', arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
();
|
||||
Loading…
Add table
Add a link
Reference in a new issue