简体   繁体   中英

WebSocket Java Error

I am still very new to java and I have been introduced to websockets recently. I am current creating a server atm and I cannot seem to be able to start the server wo getting an error

May 10, 2015 10:04:58 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:8025]
May 10, 2015 10:04:58 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
May 10, 2015 10:04:58 PM org.glassfish.tyrus.server.Server start
INFO: WebSocket Registered apps: URLs all start with ws://localhost:8025
May 10, 2015 10:04:58 PM org.glassfish.tyrus.server.Server start
INFO: WebSocket server started.
May 10, 2015 10:04:58 PM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
INFO: Stopped listener bound to [0.0.0.0:8025]
May 10, 2015 10:04:58 PM org.glassfish.tyrus.server.Server stop
INFO: Websocket Server stopped.

I am also testing on a sample program I got and am using it as reference. The difference is that the @EndPointServer annotation seems to be the problem.

Here is my code so far... WebSocket Server

package GameSever;

import org.glassfish.tyrus.server.*;

public class GameSocketServer {

    public static void main(String[] args) {
        runServer();
    }

    public static void runServer() {

        Server server = new Server("localhost", 8025, "/websockets", null, GameServerEndpoint.class);

        try {
            server.start();
        } catch(Exception e) {
            throw new RuntimeException(e);
        } finally {
            server.stop();
        }
    }
}

and here's the EndPointServer

package GameSever;

import java.util.logging.Logger;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/game")
public class GameServerEndpoint {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @OnOpen
    public void OnOpen(Session player){
        logger.info("Connected ... " + player.getId());
    }

    @OnClose
        public void OnClose(Session session, CloseReason closeReason) {
        logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
    }
}

so the problem of the code is that the Error "org.glassfish.grizzly.http.server.NetworkListener shutdownNow" seems to be preventing me to start my server. Is there anyway possible to fix this?

If you added a finally block to the end of catch block it will get executed irrespective of try or catch block

if exception doesn't occurred

try{
    //code here get executed first
}  catch(Exception e){
   //code here not executing
}finally{
   //this code executed after try block
}

if Exception occurred

try{
    //Exception occurred here
}  catch(Exception e){
   //code here get executed when exception is thrown
}finally{
   //this code executed after catch block
}

So in your case it should be like

try{
    //start here

}  catch(Exception e){
   //close(stop server) here
   //then throw the exception
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM