Created
January 21, 2012 03:16
-
-
Save wesleyhales/1651079 to your computer and use it in GitHub Desktop.
Blog: CDI Event Based Development with WebSockets and JBoss AS7
This file contains 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
window.addEventListener('memberEvent', function(e) { | |
alert(e.name + ' just registered!'); | |
}, false); |
This file contains 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
private Server server = null; | |
/** | |
* Start Embedding Jetty server when WEB Application is started. | |
* | |
*/ | |
public void contextInitialized(ServletContextEvent event) { | |
try { | |
// 1) Create a Jetty server with the 8081 port. | |
InetAddress addr = InetAddress.getLocalHost(); | |
this.server = new Server(); | |
Connector connector = new SelectChannelConnector(); | |
connector.setPort(8081); | |
connector.setHost(addr.getHostAddress()); | |
server.addConnector(connector); | |
// 2) Register ChatWebSocketHandler in the Jetty server instance. | |
ChatWebSocketHandler chatWebSocketHandler = new ChatWebSocketHandler(); | |
chatWebSocketHandler.setHandler(new DefaultHandler()); | |
server.setHandler(chatWebSocketHandler); | |
// 2) Start the Jetty server. | |
server.start(); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
} | |
.... | |
} |
This file contains 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
public void observeItemEvent(@Observes Member member) { | |
try { | |
for (ChatWebSocket webSocket : websockets) { | |
webSocket.connection.sendMessage("{\"cdievent\":{\"fire\":function(){" + | |
"eventObj.initEvent(\'memberEvent\', true, true);" + | |
"eventObj.name = '" + member.getName() + "';\n" + | |
"document.dispatchEvent(eventObj);" + | |
"}}}"); | |
} | |
} catch (IOException x) { | |
//todo - do something? | |
} | |
} |
This file contains 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
public class ChatWebSocketHandler extends WebSocketHandler { | |
private static Set<ChatWebSocket> websockets = new ConcurrentHashSet<ChatWebSocket>(); | |
public WebSocket doWebSocketConnect(HttpServletRequest request, | |
String protocol) { | |
return new ChatWebSocket(); | |
} | |
public class ChatWebSocket implements WebSocket.OnTextMessage { | |
private Connection connection; | |
public void onOpen(Connection connection) { | |
// Client (Browser) WebSockets has opened a connection. | |
// 1) Store the opened connection | |
this.connection = connection; | |
// 2) Add ChatWebSocket in the global list of ChatWebSocket | |
// instances | |
// instance. | |
getWebsockets().add(this); | |
} | |
public void onMessage(String data) { | |
// Loop for each instance of ChatWebSocket to send message server to | |
// each client WebSockets. | |
try { | |
for (ChatWebSocket webSocket : getWebsockets()) { | |
// send a message to the current client WebSocket. | |
webSocket.connection.sendMessage(data); | |
} | |
} catch (IOException x) { | |
// Error was detected, close the ChatWebSocket client side | |
this.connection.disconnect(); | |
} | |
} | |
public void onClose(int closeCode, String message) { | |
// Remove ChatWebSocket in the global list of ChatWebSocket | |
// instance. | |
getWebsockets().remove(this); | |
} | |
} | |
public static synchronized Set<ChatWebSocket> getWebsockets() { | |
return websockets; | |
} | |
} |
This file contains 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
global | |
maxconn 4096 # Total Max Connections. This is dependent on ulimit | |
nbproc 1 | |
defaults | |
mode http | |
frontend all 0.0.0.0:8080 | |
timeout client 86400000 | |
default_backend www_backend | |
acl is_websocket hdr(Upgrade) -i WebSocket | |
acl is_websocket hdr_beg(Host) -i ws | |
use_backend socket_backend if is_websocket | |
backend www_backend | |
balance roundrobin | |
option forwardfor # This sets X-Forwarded-For | |
timeout server 30000 | |
timeout connect 4000 | |
server apiserver 192.168.1.101:8080 weight 1 maxconn 4096 check | |
backend socket_backend | |
balance roundrobin | |
option forwardfor # This sets X-Forwarded-For | |
timeout queue 5000 | |
timeout server 86400000 | |
timeout connect 86400000 | |
server apiserver 192.168.1.101:8081 weight 1 maxconn 4096 check |
This file contains 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
@POST | |
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | |
@Produces(MediaType.APPLICATION_JSON) | |
public Response createMember(@FormParam("name") String name, @FormParam("email") String email, @FormParam("phoneNumber") String phone) { | |
... | |
//Create a new member class from fields | |
Member member = new Member(); | |
member.setName(name); | |
member.setEmail(email); | |
member.setPhoneNumber(phone); | |
try { | |
//Fire the CDI event | |
memberEventSrc.fire(member); |
This file contains 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
<jettyVersion>8.1.0.RC2</jettyVersion> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-continuation</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-http</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-io</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-server</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-util</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-websocket</artifactId> | |
<version>${jettyVersion}</version> | |
</dependency> | |
</dependencies> |
This file contains 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
objWebSocket.onopen = function(evt) | |
{ | |
alert("WebSocket connection opened successfully"); | |
}; | |
objWebSocket.onmessage = function(evt) | |
{ | |
alert("Message : " + evt.data); | |
}; | |
objWebSocket .onclose = function(evt) | |
{ | |
alert("WebSocket connection closed”); | |
}; |
This file contains 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
... | |
var location = "ws://192.168.1.101:8081/" | |
this._ws = new WebSocket(location); | |
.... | |
_onmessage : function(m) { | |
if (m.data) { | |
//check to see if this message is a CDI event | |
if(m.data.indexOf('cdievent') > 0){ | |
try{ | |
//$('log').innerHTML = m.data; | |
//avoid use of eval... | |
var event = (m.data); | |
event = (new Function("return " + event))(); | |
event.cdievent.fire(); | |
}catch(e){ | |
alert(e); | |
} | |
}else{ | |
//... append data in the DOM | |
} | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment