国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java實(shí)現(xiàn)簡(jiǎn)單XMPP發(fā)送消息和文件的簡(jiǎn)單例子

首先xmpp server需要安裝好配置并啟動(dòng),這里只是實(shí)現(xiàn)簡(jiǎn)單的文本發(fā)送和文件發(fā)送,接受需要用xmpp client來(lái)接收

下載地址:http://www.igniterealtime.org/downloads/index.jsp

   xmpp server(openfire)

   xmpp client(spark)

   Java XMPP client library(smack)

  smack下載完后,壓縮包里面有例子、api、和jar(smack.jar、smackx.jar、smackx-debug.jar、smackx-jingle.jar)根據(jù)需求引用這些jar

下面是一個(gè)簡(jiǎn)單的java代碼,這是做簡(jiǎn)單測(cè)試,沒有詳細(xì)優(yōu)化過(guò)(測(cè)試時(shí)發(fā)現(xiàn)個(gè)小問題:發(fā)文件需要對(duì)方加你為好友后,你發(fā)文件的時(shí)候才會(huì)顯示,不加好友的話發(fā)送時(shí)是接收方有提示但是沒看到發(fā)送過(guò)來(lái)的文件,如果是發(fā)生文本消息則沒有這個(gè)問題)

首先spark為接收方登入

然后執(zhí)行下面的java代碼

package com.test.xmpp;

import java.io.File;


import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

/**
 * @author test
 *
 */
public class TestXmpp {

    /**
     * @param args
     */
    public static void main(String[] args){
       
        String user = "user@testHost/spark";
        String host = "localhost";
        int port = 5222;
        String username = "test";
        String password = "test";
        ConnectionConfiguration config = new ConnectionConfiguration(host,port);
        config.setCompressionEnabled(true);
        config.setSASLAuthenticationEnabled(true);
       
        XMPPConnection connection = new XMPPConnection(config);
       
        try{
         connection.connect();
         
         connection.login(username, password);
         
         sendFile(user,getFile(),connection );
        // sendTextMessage(user,connection);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            connection.disconnect();
        }
       

    }
   
    public static File getFile(){
        File file = new File("D:/test.jpg");
        return file;
    }

//發(fā)送文件
    public static void sendFile(String user,File file,XMPPConnection connection )  throws Exception{
        FileTransferManager manager = new FileTransferManager(connection);
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(user);
        long timeOut = 1000000;
        long sleepMin = 3000;
        long spTime = 0;
        int rs = 0; 
      
            transfer.sendFile(file, "pls re file!");
            rs = transfer.getStatus().compareTo(FileTransfer.Status.complete);
            while(rs!=0){
                rs = transfer.getStatus().compareTo(FileTransfer.Status.complete);
                 spTime = spTime + sleepMin;
                 if(spTime>timeOut){return ;}
                Thread.sleep(sleepMin);
            }

    }

//發(fā)送文本
    public static void sendTextMessage(String user,XMPPConnection connection) throws Exception{
         Chat chat = connection.getChatManager().createChat(user, new MessageListener() {
             
                public void processMessage(Chat chat, Message message) {
                    System.out.println("Received message: " + message);
                }
            });
            chat.sendMessage("Hi Test Send Message........!");
    }
   
 

}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Smack 實(shí)現(xiàn)的Pub/Sub
《高性能的RTC服務(wù)器OpenFire》
Smack 體系結(jié)構(gòu)
Android客戶端基于XMPP的IM(openfire+asmack)的聊天工具之環(huán)境搭建及與服務(wù)器建立連接(一)
could not connect to XMPP server via smack:no response from server
openfie spark jingle語(yǔ)音插件的問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服