Apache Tomcat WebSocket Java message disconnecting client -
i have develop chat application. using websockets. have following code:
public class server { public static void main(string[] args) { org.glassfish.tyrus.server.server server = new org.glassfish.tyrus.server.server("localhost", 8025, "/ws", serverendpoint.class); try { server.start(); system.out.println("press key stop server.."); new scanner(system.in).nextline(); } catch (deploymentexception e) { throw new runtimeexception(e); } { server.stop(); } } }
and serverendpoint is:
package mechanism.chat; import static java.lang.string.format; import java.io.ioexception; import java.net.inetsocketaddress; import java.net.unknownhostexception; import java.util.arraylist; import java.util.list; import javax.websocket.encodeexception; import javax.websocket.onclose; import javax.websocket.onmessage; import javax.websocket.onopen; import javax.websocket.session; import javax.websocket.server.pathparam; import exception.businessexception; import services.chatgameservices; @javax.websocket.server.serverendpoint(value = "/chat", encoders = messageencoder.class, decoders = messagedecoder.class) public class serverendpoint { private chatgameservices chatgameservices; static list<room> roomlist = new arraylist<room>(); @onopen public void onopen(session session) { system.out.println(format("%s joined chat room.", session.getid())); // peers.add(session); } @onmessage public void onmessage(message message, session session) throws ioexception, encodeexception { string user = (string) session.getuserproperties().get("user"); this.chatgameservices = new chatgameservices(); int gameid = message.getgameid(); int userid = message.getuserid(); string type = message.gettype(); boolean hasroom = false; for(room room : roomlist){ if(gameid == room.gameid){ room.peers.add(session); hasroom = true; } } if(!hasroom){ room room = new room(); room.gameid = gameid; room.peers.add(session); roomlist.add(room); system.out.println("" + message.gettype()); } if (user == null) { session.getuserproperties().put("user", message.getsender()); } // if ("quit".equalsignorecase(message.getcontent())) { // system.out.println("quit"); // session.close(); // } system.out.println(format("[%s:%s] %s", session.getid(), message.getreceived(), message.getcontent())); //broadcast message if(message.gettype().equals("message")){ for(room room : roomlist){ if(room.gameid == gameid){ try { this.chatgameservices.createmessage(gameid, userid, message.getcontent()); } catch (businessexception e) { // todo auto-generated catch block e.printstacktrace(); } (session peer : room.peers) { if (!session.getid().equals(peer.getid())) { // not resend message sender peer.getbasicremote().sendobject(message); } } } } } else if(message.gettype().equals("disconnect")){ for(room room : roomlist){ if(room.gameid == gameid){ (session peer : room.peers) { if (session.getid().equals(peer.getid())) { // not resend message sender // peer.getbasicremote().sendobject(message); peer.close(); room.peers.remove(peer); } } } } } } @onclose public void onclose(session session) throws ioexception, encodeexception { } }
the problem is: if test local in mac works pretty fine! can send , receive messages between 2 clients. when exporting war file apache server, in windows server, doesn't work. clients can connect when 1 of them send message, other disconnected. have no idea why happening. searched lot, couldn't find anything. thank all!
Comments
Post a Comment