Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.websocket.OnClose;
- import javax.websocket.OnError;
- import javax.websocket.OnMessage;
- import javax.websocket.OnOpen;
- import javax.websocket.Session;
- import javax.websocket.server.ServerEndpoint;
- @WebServlet("/chat")
- @ServerEndpoint(value = "/chat")
- public class chat extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- private static ArrayList<Session> sesije;
- private static String ceoChat="";
- public chat() {
- super();
- if(sesije==null) sesije = new ArrayList<>();
- // TODO Auto-generated constructor stub
- }
- private static final Logger LOGGER =
- Logger.getLogger(chat.class.getName());
- @OnOpen
- public synchronized void onOpen(Session session) throws IOException {
- System.out.println("DODAT KORISNIK");
- sesije.add(session);
- LOGGER.log(Level.INFO, "New connection with client: {0}",
- session.getId());
- for (Session sess : sesije) {
- System.out.println(sess.getId());
- sess.getBasicRemote().sendText(ceoChat);
- }
- }
- @OnMessage
- public String onMessage(String message, Session session) throws IOException {
- LOGGER.log(Level.INFO, "New message from Client [{0}]: {1}",
- new Object[] {session.getId(), message});
- ceoChat+= message+"<br>";
- System.out.println("BROJ SESIJA "+sesije.size());
- System.out.println(ceoChat);
- for (Session sess : sesije) {
- sess.getBasicRemote().sendText(ceoChat);
- }
- return ceoChat;
- }
- @OnClose
- public void onClose(Session session) {
- LOGGER.log(Level.INFO, "Close connection for client: {0}",
- session.getId());
- }
- @OnError
- public void onError(Throwable exception, Session session) {
- LOGGER.log(Level.INFO, "Error for client: {0}", session.getId());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement