In the previous sections we touched briefly how the WebSocket protocol is implemented and the basic API. We'll now show the basics for creating a WebSocket aware server and registering the WebSocketApplication with the WebSocket runtime.
In this example, we'll be using Grizzly standalone (of course!). So first, we'll create the HttpServer and enable WebSocket support.
final HttpServer server = HttpServer.createSimpleServer("/var/www", 8080); final WebSocketAddOn addon = new WebSocketAddOn(); for (NetworkListener listener : server.getListeners()) { listener.registerAddOn(addon); }
That's all there is to it! Note, that you don't need to register the addon for all listeners, only those listeners that you want WebSocket support.
Next, we instantiate a WebSocketApplication implementation (we don't go into the details of the implementation itself - that comes later) and register it with the WebSocket runtime.
final WebSocketApplication chatApplication = new ChatApplication(); WebSocketEngine.getEngine().register(chatApplication);
Again, simple! Once the server is started, it will be ready to service WebSocket requests.
For a more concrete (and runnable) example, review this simple WebSocket Chat application.