ghetolay / jwamp

Java client and server implementation of WebSocket subprotocol WAMP
19 stars 6 forks source link

---- Project no more maintained ----

What's jwamp ?

JWamp is a java implementation of the WebSocket subprotocol WAMP V1.
Since on WebSocket there is no differences between client and server except for the initiation of the connection, the same API work for both client and server architecture.

This do not include a WebSocket implementation but it's easy to adapt to any WebSocket implementation.
For the moment it's only compatible with jetty WebSocket and as you can see on sources the jetty's specific part is only 3 small classes.
If you need it to work with a particulary WebSocket implementation just ask and I'll work on it.

This is the very first version so for now it's only meant to be tested, there is no stable version yet.

Since I'm still working on the Javadoc and wiki, here is a quick tutorial to keep starting :

Tutorial

Maven Configuration

First you must enable SNAPSHOT from sonatype in your pom.xml or settings.xml like this :

<repository>
    <id>snapshots-repo</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

then add jwamp dependency :

<dependency>
    <groupId>com.github.ghetolay</groupId>
    <artifactId>jwamp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

To use jwamp with jetty-websocket add :

  <dependency>
     <groupId>org.eclipse.jetty.websocket</groupId>
     <artifactId>websocket-client</artifactId> 
     <version>9.0.0.RC2</version>
  </dependency>

or

  <dependency>
     <groupId>org.eclipse.jetty.websocket</groupId>
     <artifactId>websocket-server</artifactId>
     <version>9.0.0.RC2</version>
  </dependency>

For an embedded jetty server add :

  <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>9.0.0.RC2</version>
   </dependency>

Factory

Simply use the jetty factory :

  WampJettyFactory wampFact = WampJettyFactory.getInstance();

Client connection

for a client connection there is two way of connection :

The first one will block until the connection is made and a welcome message is received or a timeout expire :

  WampWebSocket wamp = wampFact.connect(new URI("ws://URL"));

You can configure 2 kind of timeout on the WampJettyFactory :

The second one will call the listener once the welcome message is received. There is no timeout for the welcome message but you can still close the connection if the listener is not called after a amout of time.
The WampConnection received should only be used to close the connection, use the WampWebSocket from the listener to start communication.

  ResultListener<WampWebSocket> rl = new ResultListener<WampWebSocket>() {

    @Override
    public void onResult(WampWebSocket result) {

    }
  };

  WampConnection connection = wampFact.connect(new URI("ws://URL"), rl);

Server Connection

For the server just get the jetty handler (below explanation about wamp.xml):

   InputStream is = new FileInputStream("wamp.xml");
   WampWebSocketHandler wampHandler = wampFact.newWebsocketHandler(new DefaultWampParameter.SimpleServerParameter(is));

WampParameter and WampMessageHandler

WampConnection work with a collection of WampMessageHandler, each message handler will handle one or multiple type of message like CALL,CALLRESULT, SUBSCRIBE, PUBLISH....
Each WampMessageHandler should or should not be reinstanciated/reinitiated for each connection that's why I created WampParameter with the method getnewHandler(). This method manage the creation of handlers (ressources sharing...).

DefaultWampParameter contains 4 subclasses for the most commons needs.
It separated Wamp protocol in 4 group of messages :

WELCOME messages are handled by the connection and for now PREFIX messages are not supported.

So the 4 classes are :

The difference between FullClientParameter and FullServerParameter if the creation of the Event Manager.
For interoperability with other Wamp implementation a client should only handle RPC Sender and Event Sender and a server RPC Manager and Event Manager.

Wamp.xml

Wamp.xml is the easiest way of mapping actions (RPC/Event) to message handlers.
It's a first shot so it very simple. Here is a example :

  <jwamp xmlns="https://github.com/ghetolay/jwamp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://github.com/ghetolay/jwamp https://raw.github.com/ghetolay/jwamp/master/jwamp.xsd">
    <rpc>
        <action id="CallAction1" class="com.example.TestCallAction"/>
        <action id="CallAction2" class="com.example.TestCallAction2"/>
        <action id="SpecialAction" class="com.example.SpecialAction"/>
    </rpc>
    <event>
        <action id="Event1" class="com.example.TestEventAction"/>
        <action id="Event2" class="com.example.TestEventAction2"/>
        <action id="SpecialAction" class="com.example.SpecialAction"/>
                <subscribe>Event1,Event2,Event3</subscribe>
    </event>
  </jwamp>

You don't need to have both rpc and event elements.
RPC classes must implements CallAction.
Event classes must implements EventAction or extends AbstractEventAction wich is simplier to use.

The subscribe element is optional and is used for Event subscriber, it contains a list of event the connection will automatically subscribe at.

Test

see here.

These are my first steps on open project so don't hesitate to send me your feedbacks or questions !