mqtt協議實現 java服務端推送功能(三)項目中給多個用戶推送功能

接着上一篇說,上一篇的TOPIC是寫死的,然而在實際項目中要給不同用戶 也就是不同的topic進行推送 所以要寫活

package com.fh.controller.information.push;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.information.push.PushService;
import com.fh.service.system.dept.DeptService;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * @author zzm
 * mqtt給80服務端推送功能
 */
@Controller
@RequestMapping(value="/push")
public class PushController extends BaseController{
	
	@Resource(name="deptService")
	private DeptService deptService;
	
	@Resource(name="pushService")
	private PushService pushService;
	
	// tcp://MQTT安裝的服務器地址:MQTT定義的端口號
    public static final String HOST = "tcp://192.168.10.80:1883";
    // 定義一個主題
    //public static final String TOPIC = "IM/admin/Inbox";
    // 定義MQTT的ID,可以在MQTT服務配置中指定
    private static final String clientid = "server11";

    private static MqttClient client;
    private static MqttTopic topic11;
    private String userName = "admin";
    private String passWord = "admin";

    private MqttMessage message;
    
    /**
     *  構造函數
     * 
     * @throws MqttException
     */
    public PushController() throws MqttException {
        // MemoryPersistence設置clientid的保存形式,默認爲以內存保存
        client = new MqttClient(HOST, clientid, new MemoryPersistence());
        connect();
    }
    /**
     * 用來連接服務器
     */
    private void connect() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
        options.setUserName(userName);
        options.setPassword(passWord.toCharArray());
        // 設置超時時間
        options.setConnectionTimeout(10);
        // 設置會話心跳時間
        options.setKeepAliveInterval(20);
        try {
            client.setCallback(new PushCallback());
            client.connect(options);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * @param topic
     * @param message
     * @throws MqttPersistenceException
     * @throws MqttException
     */
    public void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException, MqttException {
        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();
        System.out.println("message is published completely! " + token.isComplete());
    }
	
	/**
	 * 保存
	 */
	@RequestMapping(value="/save")
	public ModelAndView save(HttpServletRequest request,
			@RequestParam(value="title",required=false) String title,
			@RequestParam(value="content",required=false) String content,
			@RequestParam(value="usernames",required=false) String usernames){
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("title", title);
		pd.put("content", content);
		String publisher = ((User)this.getRequest().getSession().getAttribute(Const.SESSION_USER)).getNAME();
		pd.put("publisher", publisher);
		pd.put("username", usernames);
		pd.put("pushtime", DateUtil.getTime());				//新增時間
		try {
			int insertNum = pushService.save(pd);
		//插入的條數
		Long id = (Long) pd.get("id");//獲取id
		//創建MQTTserver
		PushController server = new PushController();
		server.message = new MqttMessage();
		server.message.setQos(2);
		server.message.setRetained(false);
		//添加內容
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
        if(title.length() > 10) {
	    	title = title.substring(0,10) +"...";
	    }
        //content去除html標籤並縮寫
	    String contstr = "";
	    contstr = content.replaceAll("<[.[^>]]*>","");
	    contstr = contstr.replaceAll(" ", "");
	    if(contstr.length() > 15) {
	    	contstr = contstr.substring(0,15) +"...";
	    }
	    //拼接要傳入的json字符串
	    JSONObject jsonObj = new JSONObject();
	    jsonObj.put("title", title);
	    jsonObj.put("message", contstr);
	    jsonObj.put("time", df.format(new Date()));
	    jsonObj.put("type", 4);
	    jsonObj.put("Id", id);
		//放入mqtt server  轉utf-8防止亂碼
		server.message.setPayload(jsonObj.toString().getBytes("UTF-8"));
		
		String[] names = usernames.split(",");
		for(String i:names) {
			//設置topic
			topic11 = client.getTopic("IM/"+i+"/Inbox");
			//推送
			server.publish(server.topic11, server.message);
			System.out.println(server.message.isRetained() + "------ratained狀態");
		}
        
		mv.addObject("msg","success");
		mv.setViewName("information/push/push_list");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
}

推送的內容  按app要求需要放到json格式的字符串中,爲了防止亂碼 需要用utf8轉換一下

topic因爲需要設置爲動態,所以根據前端提交的name值拼接作爲topic名稱

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章