Last active
August 29, 2015 13:56
-
-
Save annagapuz/9192234 to your computer and use it in GitHub Desktop.
Integrating Atmosphere with Grails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Long-Polling is used in this example b/c of IE7 | |
// Add atmosphere to ApplicationResources.groovy | |
modules = { | |
... | |
atmosphere { | |
dependsOn 'jquery' | |
resource url:'js/atmosphere.js' // used jQuery atmosphere minified | |
} | |
} | |
// ... in order to pull into GSP using | |
<g:javascript library="atmosphere" /> | |
// BuildConfig.groovy | |
grails.tomcat.nio = true | |
dependencies { | |
... | |
compile 'org.atmosphere:atmosphere-runtime-native:2.1.0' | |
} | |
// Config.groovy | |
Make sure atmosphere URL path is accessible via security settings | |
// web.xml | |
<!-- Atmosphere servlet --> | |
<servlet> | |
<description>AtmosphereServlet</description> | |
<servlet-name>AtmosphereServlet</servlet-name> | |
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class> | |
<init-param> | |
<!-- A list, separated by comma, of package names to scan when looking for Atmosphere's annotations. --> | |
<param-name>org.atmosphere.cpr.packages</param-name> | |
<param-value>com.path.to.find.any.annotated.classes</param-value> | |
</init-param> | |
<load-on-startup>0</load-on-startup> | |
<async-supported>true</async-supported> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>AtmosphereServlet</servlet-name> | |
<url-pattern>/atmourl/*</url-pattern> | |
</servlet-mapping> | |
// All init-params available | |
http://atmosphere.github.io/atmosphere/apidocs/org/atmosphere/cpr/ApplicationConfig.html | |
// Added as a service, but also worked when added as a controller | |
// @ManagedService - path should match URL pattern for defined atmosphere servlet in web.xml | |
// https://github.com/Atmosphere/atmosphere/wiki/Getting-Started-with-the-ManagedService-Annotation | |
import org.atmosphere.config.service.Disconnect | |
import org.atmosphere.config.service.ManagedService | |
import org.atmosphere.config.service.Message | |
import org.atmosphere.config.service.Ready | |
import org.atmosphere.cpr.AtmosphereResource | |
import org.atmosphere.cpr.AtmosphereResourceEvent | |
@ManagedService(path = "/atmourl") | |
class UiCommService { | |
@Ready | |
public void onReady(final AtmosphereResource r) { | |
log.info("Browser {} connected.", r.uuid()); | |
} | |
@Disconnect | |
public void onDisconnect(AtmosphereResourceEvent event) { | |
if (event.isCancelled()) { | |
log.info("Browser {} unexpectedly disconnected", event.getResource().uuid()); | |
} else if (event.isClosedByClient()) { | |
log.info("Browser {} closed the connection", event.getResource().uuid()); | |
} | |
} | |
//@org.atmosphere.config.service.Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class}) | |
@org.atmosphere.config.service.Message | |
public Message onMessage(Message message) throws IOException { | |
log.info("{} just sent {}", message.getAuthor(), message.getMessage()); | |
return message; | |
} | |
} | |
// Broadcast from anywhere in code | |
// broadcast message to UI | |
Broadcaster b = BroadcasterFactory.getDefault().lookup("/atmourl") | |
log.debug("Broadcaster: " + b) | |
log.debug("Atmo Resources: " + b.getAtmosphereResources()) | |
b.broadcast(msg) | |
// Embedded JS in GSP, or can be in separate JS file | |
// https://github.com/Atmosphere/atmosphere/wiki/jQuery.atmosphere.js-atmosphere.js-API | |
<g:javascript> | |
var socket = $.atmosphere; | |
var subSocket; | |
var uicommUrl = document.location.protocol + '//' + document.location.hostname + ':' + document.location.port + '/context/atmourl'; | |
var request = { | |
url: uicommUrl, | |
contentType : "application/json", | |
//reconnectInterval: 1000, | |
timeout : 15000, | |
logLevel: 'error', | |
transport : "long-polling" , | |
fallbackTransport: 'long-polling'}; | |
request.onOpen = function(response) { | |
console.log("atmosphere onOpen " + response.transport) | |
// Carry the UUID. This is required if you want to call subscribe(request) again. | |
request.uuid = response.request.uuid; | |
}; | |
request.onClientTimeout = function(response) { | |
console.log("atmosphere onClientTimeout") | |
}; | |
request.onClose = function(response) { | |
console.log("atmosphere onClose") | |
}; | |
request.onError = function(response) { | |
console.log("atmosphere onError") | |
}; | |
request.onReconnect = function(request, response) { | |
console.log("atmosphere onReconnect") | |
}; | |
request.onMessage = function (response) { | |
console.log("atmosphere onMessage " + response.responseBody) | |
$('div#divToAlterWithResponse').text(response.responseBody) | |
}; | |
subSocket = socket.subscribe(request); | |
</g:javascript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment