WebSocket API Chat Room using JSR 356

From Resin 4.0 Wiki

Revision as of 00:00, 9 November 2012 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

ChatServer

 
package com.example;

import java.net.URI;
import java.net.URISyntaxException;

import javax.inject.Inject;
import javax.net.websocket.ContainerProvider;
import javax.net.websocket.DefaultServerConfiguration;
import javax.net.websocket.ServerContainer;
import javax.net.websocket.ServerEndpointConfiguration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


@WebListener
public class ChatServer implements ServletContextListener{

    @Inject ChatRoom room;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        ServerEndpointConfiguration serverConfiguration;
        try {
            serverConfiguration = new DefaultServerConfiguration(new URI("/chat"));
            ServerContainer serverContainer = ContainerProvider.getServerContainer();
            serverContainer.publishServer(new ChatServerEndpoint(room),serverConfiguration);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

    }
}

Personal tools
TOOLBOX
LANGUAGES