1

我有一个示例 WebSocket 程序,它在下面运行良好

当用户关闭浏览器或有任何异常或任何断开连接时,将调用 onClose 方法

我的问题是,是否有可能从程序中知道调用 onClose 的原因是什么?请分享您的观点,感谢您的阅读。

public class Html5Servlet extends WebSocketServlet {

    private AtomicInteger index = new AtomicInteger();

    private static final List<String> tickers = new ArrayList<String>();
    static{
        tickers.add("ajeesh");
        tickers.add("peeyu");
        tickers.add("kidillan");
        tickers.add("entammo");
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public WebSocket doWebSocketConnect(HttpServletRequest req, String resp) {
        //System.out.println("doWebSocketConnect");
        return new StockTickerSocket();
    }
    protected String getMyJsonTicker() throws Exception{
        return "";
    }
    public class StockTickerSocket implements WebSocket.OnTextMessage{
        private Connection connection;
        private Timer timer; 


        @Override
        public void onClose(int arg0, String arg1) {
            System.out.println("onClose called!"+arg0);
        }

        @Override
        public void onOpen(Connection connection) {
            //System.out.println("onOpen");
            this.connection=connection;
            this.timer=new Timer();
        }

        @Override
        public void onMessage(String data) {
            //System.out.println("onMessage");
            if(data.indexOf("disconnect")>=0){
                connection.close();
                timer.cancel();
            }else{
                sendMessage();

            }           
        }




        public void disconnect() {

            System.out.println("disconnect called");
        }

        public  void onDisconnect()
        {
            System.out.println("onDisconnect called");
        }


        private void sendMessage() {

            if(connection==null||!connection.isOpen()){
                //System.out.println("Connection is closed!!");
                return;
            }
            timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        try{
                            //System.out.println("Running task");
                            connection.sendMessage(getMyJsonTicker());
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }, new Date(),5000);
        }

    }
}
4

1 回答 1

1

签名onClose如下...

    @Override
    public void onClose(int closeCode, String closeReason) {
        System.out.println("onClose called - statusCode = " + closeCode);
        System.out.println("                 reason     = " + closeReason);
    }

int closeCode任何已注册的关闭状态代码在哪里。

并且String closeReason是可选的(根据协议规范)关闭原因消息。

于 2013-04-15T04:46:19.517 回答