【问题标题】:WebSocket Java ErrorWebSocket Java 错误
【发布时间】:2015-05-11 02:12:37
【问题描述】:

我对 java 还是很陌生,最近才接触到 websockets。我目前正在创建服务器 atm,但我似乎无法启动服务器 w.o 出现错误

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.

我还在测试我得到的示例程序并将其用作参考。不同之处在于@EndPointServer 注解似乎是问题所在。

到目前为止,这是我的代码... WebSocket 服务器

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();
        }
    }
}

这里是 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));
    }
}

所以代码的问题是错误“org.glassfish.grizzly.http.server.NetworkListener shutdownNow”似乎阻止我启动我的服务器。有没有办法解决这个问题?

【问题讨论】:

  • 您似乎正在调用.start(),然后立即调用.stop()(在finally 块中。)这可能是服务器启动然后停止的原因。 (没有记录任何错误,应用程序正在执行您的代码指定的操作。)您的代码只是启动然后停止服务器是否有原因?
  • 好吧,我正在尝试这样做,如果 start.sever() 不起作用,它会抛出异常并停止服务器
  • @NoobestPros 好吧,如果您打算这样做,那么您可能希望将 stop 函数放在 catch 语句中。因为如果你把它放在finally语句里面,里面的代码无论如何都会被执行。

标签: java websocket


【解决方案1】:

如果你在 catch 块的末尾添加了 finally 块,它将被执行而不管 try 或 catch 块

如果没有发生异常

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

如果发生异常

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

所以你的情况应该是这样的

try{
    //start here

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多