Created
July 27, 2012 00:40
-
-
Save tantaman/3185457 to your computer and use it in GitHub Desktop.
Embedding CometD in an existing JVM
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
<!doctype html> | |
<html> | |
<head> | |
<!-- include Faye --> | |
<script type="text/javascript" src="scripts/faye-browser.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
// Connect to our Java CometD server | |
var client = new Faye.Client('http://localhost:8080/bayeux'); | |
// Subscribe for the /data channel | |
client.subscribe('/data', function(msg) { | |
// log the data we got | |
console.log(msg); | |
}); | |
client.publish('/data', {hello: "from myself?"}); | |
</script> | |
</body> | |
</html> |
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
Jetty: | |
1. Download Jetty | |
http://download.eclipse.org/jetty/ | |
2. Extract to some location via winrar or tar -zxf | |
Cometd: | |
1. Download the latest Cometd (2.4.3 as of this writing) | |
http://download.cometd.org/?C=M;O=A | |
2. extract | |
slf4j: | |
1. download | |
2. extract | |
Creating the Eclipse Project: | |
1. Create a new project | |
2. Add all jetty-8/lib jars as dependencies (may not need all..) | |
3. Add the following cometd libs: | |
cometd/cometd-java/cometd-java-server/target/*.jar | |
cometd/cometd-java/cometd-websocket-jetty/target/*.jar | |
cometd/cometd-java/bayeux-apit/target/*.jar | |
cometd/cometd-java/cometd-java-common/*.jar | |
4. Add slf4j | |
slf4j/slf4j-api-1.6.6.jar | |
slf4j/slf4j-simple-1.6.6.jar |
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
package com.tantaman.cometd.embed.example; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
import org.cometd.bayeux.server.BayeuxServer; | |
import org.cometd.bayeux.server.ConfigurableServerChannel; | |
import org.cometd.bayeux.server.ServerChannel; | |
import org.cometd.server.CometdServlet; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
import org.eclipse.jetty.servlet.ServletHolder; | |
import sun.misc.BASE64Encoder; | |
public class Main { | |
private static final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); | |
public static void main(String [] args) throws Exception { | |
// Configure Jetty | |
Server server = new Server(8080); | |
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); | |
context.setContextPath("/"); | |
server.setHandler(context); | |
// Configure Cometd | |
CometdServlet servlet = new CometdServlet(); | |
context.addServlet(new ServletHolder(servlet), "/bayeux"); | |
// Start Jetty | |
server.start(); | |
// Create a channel for our Java server to publish on | |
BayeuxServer bayeux = servlet.getBayeux(); | |
String channelName = "/data"; | |
bayeux.createIfAbsent(channelName, new ServerChannel.Initializer() | |
{ | |
public void configureChannel(ConfigurableServerChannel channel) | |
{ | |
channel.setPersistent(true); | |
} | |
}); | |
final ServerChannel channel = bayeux.getChannel(channelName); | |
// publish some random data | |
byte [] bytes = new byte[40]; | |
for (int i = 0; i < bytes.length; ++i) { | |
bytes[i] = (byte)(Math.random() * 255); | |
} | |
BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder(); | |
final String data = base64Encoder.encode(bytes); | |
final Map<String, String> firstData = new HashMap<>(); | |
firstData.put("a", "b"); | |
firstData.put("something", "val"); | |
// Maps are converted to JavaScript objects | |
final Map<String, String> secondData = new HashMap<>(); | |
secondData.put("base64data", data); | |
exec.scheduleAtFixedRate(new Runnable() { | |
@Override | |
public void run() { | |
if (Math.random() < 0.5) { | |
channel.publish(null, firstData, "1"); | |
} else { | |
channel.publish(null, secondData , "2"); | |
} | |
} | |
}, 1, 1, TimeUnit.SECONDS); | |
// Keep running until the server dies | |
server.join(); | |
} | |
} |
Hi,
Got error like follows
cometd.js:892 WebSocket connection to 'ws://localhost:8080/bayeux' failed: Error during WebSocket handshake: Unexpected response code: 400
Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi can you please share the faye-browser.js please. I tried to download after some search in the internet. But it says:
[HTTP/1.1 500 Server Error 8ms]