Java Reference
In-Depth Information
Creating our first endpoint
Let's start with a simple example:
package com.packtpub.wflydevelopment.chapter8.boundary;
import javax.websocket.EndpointConfig;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@ServerEndpoint("/hello")
public class HelloEndpoint {
@OnOpen
public void open(
Session
session,
EndpointConfig
conf)
throws IOException {
session.getBasicRemote().sendText("Hi!");
}
}
Java EE 7 specification has taken into account developer friendliness, which can be clearly
seen in the given example. In order to define your WebSocket endpoint, you just need a
few annotations on a
Plain Old Java Object
(
POJO
). The first annotation
@Server-
Endpoint("/hello")
defines a path to your endpoint. It's a good time to discuss the
endpoint's full address. We placed this sample in the application named
ticket-
agency-websockets
. During the deployment of application, you can spot information
in the WildFly log about endpoints creation, as shown in the following command line:
02:21:35,182 INFO [io.undertow.websockets.jsr] (MSC service
thread 1-7) UT026003: Adding annotated server endpoint class
com.packtpub.wflydevelopment.chapter8.boundary.FirstEndpoint
for path /hello
02:21:35,401 INFO
[org.jboss.resteasy.spi.ResteasyDeployment] (MSC service
thread 1-7) Deploying javax.ws.rs.core.Application: class
com.packtpub.wflydevelopment.chapter8.webservice.JaxRsActivator$Proxy$_$$_WeldClientProxy