简体   繁体   English

WebSocket Java错误

[英]WebSocket Java Error

I am still very new to java and I have been introduced to websockets recently. 我对Java还是很陌生,最近已被介绍给websockets。 I am current creating a server atm and I cannot seem to be able to start the server wo getting an error 我当前正在创建服务器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.

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. 区别在于@EndPointServer批注似乎是问题所在。

Here is my code so far... WebSocket Server 到目前为止,这是我的代码... 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();
        }
    }
}

and here's the EndPointServer 这是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. 因此,代码的问题是错误“ org.glassfish.grizzly.http.server.NetworkListener shutdownNow”似乎阻止了我启动服务器。 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 如果在catch块的末尾添加了finally块,则无论try还是catch块如何,都会执行该块

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
}

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

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