简体   繁体   English

Websocket:是否可以从程序中知道调用onClose的原因是什么

[英]Websocket : Is it possible to know from the program what is the reason for onClose being called

I have a Sample WebSocket Program whown below which works fine 我有一个下面的示例WebSocket程序,可以正常工作

When ever the user closes the browser or if there is any excetion Or any disconnect , the onClose Method is being called 每当用户关闭浏览器或出现任何异常或断开连接时,就会调用onClose方法

My question is that , Is it possible to know from the program what is the reason for onClose being called ?? 我的问题是,是否可以从程序中知道onClose被调用的原因是什么? Please share your views , Thanks for reading . 请分享您的意见,感谢您的阅读。

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

    }
}

The signature for onClose is the following ... onClose的签名如下:

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

Where int closeCode is any of the registered Close Status Codes . 其中int closeCode是任何已注册的关闭状态代码

And String closeReason is an optional ( per protocol spec ) close reason message. String closeReason是可选的( 根据协议规范 )关闭原因消息。

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

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