繁体   English   中英

WebSocket Java错误

[英]WebSocket Java Error

我对Java还是很陌生,最近已被介绍给websockets。 我当前正在创建服务器atm,但似乎无法启动服务器而出现错误

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”似乎阻止了我启动服务器。 反正有可能解决此问题吗?

如果在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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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