当前位置:首页 > 开发 > 移动开发 > 正文

smack - PacketWriter

发表于: 2013-03-07   作者:追求幸福   来源:转载   浏览次数:
摘要: 转自: http://blog.csdn.net/wqhjfree/article/details/7194416 PacketWriter是在上篇在介绍initConnection()方法中创建的,那么详细看下PacketWriter的实现: protected PacketWriter(XMPPConnection connection) { this.queu
转自: http://blog.csdn.net/wqhjfree/article/details/7194416

PacketWriter是在上篇在介绍initConnection()方法中创建的,那么详细看下PacketWriter的实现:
protected PacketWriter(XMPPConnection connection) {
        this.queue = new ArrayBlockingQueue<Packet>(500, true);
        this.connection = connection;
        init();
 }




protected void init() {
        this.writer = connection.writer;
        done = false;
        lastActive = System.currentTimeMillis();
        
        //开辟一个写网络流数据线程
        writerThread = new Thread() {
            public void run() {
                writePackets(this);
            }
        };

        writerThread.setName("Smack Packet Writer (" + connection.connectionCounterValue + ")");
        writerThread.setDaemon(true);

    }



/**
     * 向网络流写数据包
     * @param thisThread
     */
    private void writePackets(Thread thisThread) {
        try {
            // Open the stream.
            openStream();
            // Write out packets from the queue.
            while (!done && (writerThread == thisThread)) {
                Packet packet = nextPacket();
                if (packet != null) {
                    synchronized (writer) {
                        writer.write(packet.toXML());
                        writer.flush();
                        // Keep track of the last time a stanza was sent to the
                        // server
                        lastActive = System.currentTimeMillis();
                    }
                }
            }

            synchronized (writer) {
                while (!queue.isEmpty()) {
                    Packet packet = queue.remove();
                    writer.write(packet.toXML());
                }
                writer.flush();
            }

            queue.clear();
            writer.write("</stream:stream>");
            writer.flush();

        } catch (IOException ioe) {
            if (!done) {
                done = true;
                connection.packetReader.notifyConnectionError(ioe);
            }
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception e) {
            }
        }
    }



在PacketWriter构造方法中传递参数XMPPConnection作为PacketWriter成员变量, 然后调用init()方法 ,而在init()方法中, 开辟一个线程专门用于向服务器写数据. writerPackets()里面是一个无限循环体, 不断的从消息队列queue读取pakcet消息读取完之后调用wirter.flush()方法, 将消息发送的服务器. 这个过程就完成了终端完服务器发送消息的过程.

接下来是, 终端如何与云端保持连接呢, 那肯定是发送心跳包咯. 我们来看下PacketWriter是如何发送心跳包的:

/**
     * A TimerTask that keeps connections to the server alive by sending a space
     * character on an interval.
     */
    private class KeepAliveTask implements Runnable {

        private int delay;
        private Thread thread;

        public KeepAliveTask(int delay) {
            this.delay = delay;
        }

        protected void setThread(Thread thread) {
            this.thread = thread;
        }
        
        public void run() {
            try {
                // Sleep 15 seconds before sending first heartbeat. This will give time to
                // properly finish TLS negotiation and then start sending heartbeats.
                Thread.sleep(15000);
            }
            catch (InterruptedException ie) {
                // Do nothing
            }
            while (!done && keepAliveThread == thread) {
                synchronized (writer) {
                    // Send heartbeat if no packet has been sent to the server for a given time
                    if (System.currentTimeMillis() - lastActive >= delay) {
                        try {
                            writer.write(" ");
                            writer.flush();
                        }
                        catch (Exception e) {
                            // Do nothing
                        }
                    }
                }
                try {
                    // Sleep until we should write the next keep-alive.
                    Thread.sleep(delay);
                }
                catch (InterruptedException ie) {
                    // Do nothing
                }
            }
        }
    }


心跳发送类: KeepAliveTask 是一个自定义线程, 它定义了心跳发送的时间间隔, 在其run方法里面一个无限循环体每隔delay毫秒就向云端发发送一个空消息(当然, 如需要你可以根据项目需求自定义一个心跳包). 整个PacketWriter介绍基本到此结束, 在你应用登陆成功之后就可以开启KeepAliveTask 线程定时发送心跳包到服务器保持终端与服务器的长连接.

smack - PacketWriter

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
OpenFire 是采用Java开发的基于XMPP(Jabber)协议,开源实时协作(RTC)服务器。 Smack 是用 Java
OpenFire 是采用Java开发的基于XMPP(Jabber)协议,开源实时协作(RTC)服务器。 Smack 是用 Java
OpenFire 是采用Java开发的基于XMPP(Jabber)协议,开源实时协作(RTC)服务器。 Smack 是用 Java
openfire的文件发送有两种: XMPP支持两种文件流传输协议,SOCKS5 Bytestreams和 In-Band Bytestrea
smack是什么?具体百度一下。我的理解也不深,我理解其实就是XMPP协议的实现。都是浮云,一切都是工
XmpPullParser   鉴于xmpp协议都是以xml格式来传输,因此源码中解析协议都是用到XmpPullParser来
基于openfire_3_10_0、smack_4_1_1 的聊天推送demo. 前提:在最新版本中通过smack类库实现聊天内容
1:关于smack与tigase的用法跟作用请大家自己去网上查看相关资料,这里就不做描述了。 PS:这篇文章
上一篇:基于xmpp openfire smack开发openfire之介绍和部署[1] 关于Smack编程库,前面我们提到,它
本文转载http://blog.csdn.net/shimiso 关于Smack编程库,前面我们提到,它是面向Java端的api,主要
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号