SAE最近新推出了CHANEL服務,就是由服務器端發起的主動推送服務。經過這個服務就能夠簡單的實如今線聊天,在線客服等。
這裏對具體能開發的應用就不贅述了。主要來講一下如何實現。
首先須要下載SAE服務支持的JAR包。下載地址<a href="http://sae4java.sinaapp.com/lib/sae-local-1.1.0.jar" title="sae-local-1.1.0.jar 下載" target="_blank"></a>並在工程中添加此JAR包在LIB中。
下面就是個人程序部分,
一、 INDEXSEVLET,這個你們自定,也能夠直接把這部分程序寫在INDEX.JSP中。這裏就是爲用戶建立一個CHANEL對象,併產生一個CHANEL服務的URL。
javascript
Java代碼
import java.io.IOException;java
import java.io.PrintWriter;api
import java.util.UUID;服務器
import javax.servlet.ServletException;微信
import javax.servlet.http.HttpServlet;app
import javax.servlet.http.HttpServletRequest;微信公衆平臺
import javax.servlet.http.HttpServletResponse;dom
import com.sina.sae.channel.SaeChannel;socket
public class IndexServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br />
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br />
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String user = UUID.randomUUID().toString().replace("-", "");
//request.getSession().setAttribute("user", user);
SaeChannel channel = new SaeChannel();
String user="bingobing";
String name = user;//channel名稱(若是是一對一的推送服務,那麼每一個用戶的CHANNEL名稱應該是不一樣的,當推送消息時就是依據這個名字)
int duration = 1000;//channel過時時間(單位爲秒)
String url = channel.createChannel(name, duration);//返回值爲WebSocket的url
response.sendRedirect("index.jsp?url="+url);
}
}
二、在INDEX.JSP頁面中根據以前產生的CHANEL服務URL將JS客戶端鏈接上CHANEL服務
Java代碼
< %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
< !DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="http://channel.sinaapp.com/api.js"></script>
<script type="text/javascript">
var socket = new sae.Channel('< %=request.getParameter("url") %>');//此處url爲上面建立channel返回的url
socket.onopen = function(){
alert("open socket!");
}
//設置channel打開事件,
socket.onmessage = function(message){//設置接收CHANNEL服務推送消息事件
//其中message對象的data字段爲channel發送的消息,如上範例爲"this message"
alert(message.data);
}
</script>
</head>
<body>
</body>
</html>
三、經過服務端向指定的CHANNEL客戶端推送消息
Java代碼
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sina.sae.channel.SaeChannel;
public class ChanelService extends HttpServlet {
/**
* The doGet method of the servlet. <br />
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br />
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
SaeChannel channel = new SaeChannel();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String name=(String) request.getSession().getAttribute("user");
String name="bingobing";
int nums=channel.sendMessage(name, "aaaaa");//經過指定的CHANNEL服務名稱,向使用該CHANNEL服務的客戶端推送消息
response.getWriter().print(nums);
}
}
當經過網頁訪問ChanelService 時,全部使用BINGOBING名稱的CHANNEL客戶端都會收到一條內容爲AAAAA的消息。客戶端並無進行任何操做,徹底由服務器主動推送至客戶端。
作了個簡單的DEMO而已,具體的應用尚未想好,不過實現方式真的很簡單,這裏就想到了以前爲一個朋友作的微信公衆平臺的一個在線客服功能模塊,客戶要求,當微信公衆平臺關注用戶向微信公衆號發送指定規則拼接的客服請求時,客服人員能經過自行開發管理平臺的後臺客服處理界面,能實時接收到用戶的客服請求內容,並即時向客戶進行返回,當時的實現方式是經過AJAX輪詢的方式,來實現實時性要求,不過如今有了CHANNEL服務,就很容易實現了。
當客服人員登陸管理平臺的客服處理界面後,建立一個指定名稱的CHANNEL服務,並連接上此CHANNEL服務。當微信公衆號關注分析提出在線客服請求後,將客服請求內容經過調用CHANNEL服務,向指定名稱的服務發送消息,這樣客服就能實時獲取到粉絲的請求,並進行反饋,這樣的好處在於一、避免了AJAX輪詢致使的反覆請求服務器,減小了服務器資源的開銷;二、AJAX輪詢的請求頻率有必定的延遲,沒法作到實時,只能作到一個很高頻率的循環而已,用了CHANNEL服務,能實現實時性要求。