Contact

jWebSocket Headquarter

Innotrade GmbH

An Vieslapp 29

52134 Herzogenrath

Germany

Maven

Introduction to jWebSocket Maven Repository

In Maven terminology, a repository is a place where the project jars, plugins or any other project specific artifacts are stored and can be used by any Maven user as long as the server where the repository is published is reachable over the internet.

How to use our repository?

Make sure that you have Maven installed in your computer or an IDE that has Maven support like Netbeans or Eclipse.

For the following example we will use Netbeans as development IDE.
  1. Create a basic Maven Project.
  2. Open the pom.xml from the Project Directory:
3. And include the following lines under the <project> configuration:
<repositories>
	<repository>
		<id>jWebSocket-Repo</id>
		<name>jWebSocket Repository</name>
		<url>http://mvn.jwebsocket.org/</url>
	</repository>
</repositories>
4. Now your project is ready to use the jWebSocket ".jar" dependencies, so let's include the jWebSocket Server:
<dependencies>
	<dependency>
		<artifactId>jWebSocketServer</artifactId>
		<groupId>org.jwebsocket</groupId>
		<version>1.0</version>
		<type>jar</type>
	</dependency>
</dependencies>

Once we included the dependencies in our pom.xml then we are ready to start creating our first PlugIn. A PlugIn is a simple Java Class that extends from TokenPlugIn, so, in other words, it will be the main class of your application if you plan to create a jWebSocket app.
Let's create a first example of a PlugIn with the name MyPlugIn, in the following code you will see that the PlugIn we are creating only is able to register and unregister clients, as well as broadcast to all registered clients that a new client is online:

package com.jwebsocketmavenproject;

import java.util.List;
import javolution.util.FastList;
import org.jwebsocket.api.PluginConfiguration;
import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.config.JWebSocketServerConstants;
import org.jwebsocket.kit.CloseReason;
import org.jwebsocket.kit.PlugInResponse;
import org.jwebsocket.plugins.TokenPlugIn;
import org.jwebsocket.token.Token;
import org.jwebsocket.token.TokenFactory;

/**
 *
 * @author Victor
 */
public class MyPlugIn extends TokenPlugIn {

	private final String NS = JWebSocketServerConstants.NS_BASE + ".plugins.myplugin";
	private List<WebSocketConnector> mClients;

	public MyPlugIn(PluginConfiguration aConfiguration) {
		super(aConfiguration);
		// Setting the namespace to the application
		this.setNamespace(NS);
		// Initializing the list
		mClients = new FastList<>();
	}

	@Override
	public void processToken(PlugInResponse aResponse, WebSocketConnector aConnector, Token aToken) {
		// The token type coming from the client side
		if ("register".equals(aToken.getType())) {
			register(aConnector, aToken);
			// Whenever a user registers for example we could send a "registered" 
			// token to all registered clients so they know who is connected
			// This could be useful maybe in a chat application, etc...
			Token lBroadcastToken = TokenFactory.createToken(NS, "registered");
			lBroadcastToken.setString("msg", "Client " + aConnector.getUsername() + " registered successfully!");
			broadcast(lBroadcastToken);
		}
	}

	@Override
	public void connectorStopped(WebSocketConnector aConnector, CloseReason aCloseReason) {
		// If the connection is closed, we could remove the connector from the list
		unregister(aConnector);
	}

	private void register(WebSocketConnector aConnector, Token aToken) {
		if (!mClients.isEmpty() && !mClients.contains(aConnector)) {
			// Registered
			mClients.add(aConnector);
		}
	}

	private void unregister(WebSocketConnector aConnector) {
		if (!mClients.isEmpty() && mClients.contains(aConnector)) {
			// Unregistered
			mClients.remove(aConnector);
		}
	}

	private void broadcast(Token aToken) {
		for (WebSocketConnector lConnector : mClients) {
			// Broadcasting a token to all the registered clients
			sendToken(lConnector, aToken);
		}
	}
}

Once we have the example code we "Clean and Build" our new Application, and we will see the results as below:

cd C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject;  "JAVA_HOME=C:\\Program Files\\Java\\jdk1.7.0_25"  M2_HOME=C:\\maven cmd /c "\"\"C:\\maven\\bin\\mvn.bat\" -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.0\\java\\maven-nblib\\netbeans-eventspy.jar\"  -Dfile.encoding=UTF-8 clean install\""
Scanning for projects...


------------------------------------------------------------------------
Building jWebSocketMavenProject 1.0-SNAPSHOT
------------------------------------------------------------------------
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServer/1.0-RC3/jWebSo...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServer/1.0-RC3/jWebSo... (13 KB at 16.7 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketCommon/1.0-RC3/jWebSo...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketCommon/1.0-RC3/jWebSo... (8 KB at 59.9 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketDynamicSQL/1.0-RC3/jW...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketDynamicSQL/1.0-RC3/jW... (5 KB at 37.8 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServerAPI/1.0-RC3/jWe...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServerAPI/1.0-RC3/jWe... (10 KB at 78.7 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketSSO/1.0-RC3/jWebSocke...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketSSO/1.0-RC3/jWebSocke... (6 KB at 16.9 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketLDAP/1.0-RC3/jWebSock...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketLDAP/1.0-RC3/jWebSock... (4 KB at 12.9 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketJMSEndPoint/1.0-RC3/j...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketJMSEndPoint/1.0-RC3/j... (3 KB at 20.3 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServer/1.0-RC3/jWebSo...
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketCommon/1.0-RC3/jWebSo...
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketDynamicSQL/1.0-RC3/jW...
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServerAPI/1.0-RC3/jWe...
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketSSO/1.0-RC3/jWebSocke...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketSSO/1.0-RC3/jWebSocke... (21 KB at 57.3 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketLDAP/1.0-RC3/jWebSock...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketLDAP/1.0-RC3/jWebSock... (19 KB at 144.7 KB/sec)
Downloading: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketJMSEndPoint/1.0-RC3/j...

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServerAPI/1.0-RC3/jWe... (116 KB at 203.1 KB/sec)

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketJMSEndPoint/1.0-RC3/j... (40 KB at 162.9 KB/sec)

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketCommon/1.0-RC3/jWebSo... (144 KB at 138.5 KB/sec)

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketDynamicSQL/1.0-RC3/jW... (565 KB at 160.4 KB/sec)

Downloaded: http://mvn.jwebsocket.org/org/jwebsocket/jWebSocketServer/1.0-RC3/jWebSo... (19122 KB at 459.8 KB/sec)

--- maven-clean-plugin:2.4.1:clean (default-clean) @ jWebSocketMavenProject ---
Deleting C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\target

--- maven-resources-plugin:2.5:resources (default-resources) @ jWebSocketMavenProject ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\src\main\resources

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ jWebSocketMavenProject ---
Compiling 1 source file to C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\target\classes

--- maven-resources-plugin:2.5:testResources (default-testResources) @ jWebSocketMavenProject ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\src\test\resources

--- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ jWebSocketMavenProject ---
No sources to compile

--- maven-surefire-plugin:2.10:test (default-test) @ jWebSocketMavenProject ---
No tests to run.
Surefire report directory: C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0


--- maven-jar-plugin:2.3.2:jar (default-jar) @ jWebSocketMavenProject ---
Building jar: C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\target\jWebSocketMavenProject-1.0-SNAPSHOT.jar

--- maven-install-plugin:2.3.1:install (default-install) @ jWebSocketMavenProject ---
Installing C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\target\jWebSocketMavenProject-1.0-SNAPSHOT.jar to C:\Users\Victor\.m2\repository\com\jWebSocketMavenProject\1.0-SNAPSHOT\jWebSocketMavenProject-1.0-SNAPSHOT.jar
Installing C:\Users\Victor\Documents\NetBeansProjects\jWebSocketMavenProject\pom.xml to C:\Users\Victor\.m2\repository\com\jWebSocketMavenProject\1.0-SNAPSHOT\jWebSocketMavenProject-1.0-SNAPSHOT.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 48.682s
Finished at: Fri Dec 12 19:51:12 EET 2014
Final Memory: 23M/228M
------------------------------------------------------------------------

And at this point we already have jWebSocket Server ready to start developing our first application :)

Copyright © 2013 Innotrade GmbH. All rights reserved.