SAE Channel 服務 JAVA 最簡單DEMO及使用感覺

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代碼 html

  1. import java.io.IOException;java

  2. import java.io.PrintWriter;api

  3. import java.util.UUID;服務器

  4. import javax.servlet.ServletException;微信

  5. import javax.servlet.http.HttpServlet;app

  6. import javax.servlet.http.HttpServletRequest;微信公衆平臺

  7. import javax.servlet.http.HttpServletResponse;dom

  8. import com.sina.sae.channel.SaeChannel;socket

  9. public class IndexServlet extends HttpServlet {

  10. /**

  11. * The doGet method of the servlet. <br />

  12. *

  13. * This method is called when a form has its tag value method equals to get.

  14. *

  15. * @param request the request send by the client to the server

  16. * @param response the response send by the server to the client

  17. * @throws ServletException if an error occurred

  18. * @throws IOException if an error occurred

  19. */

  20. public void doGet(HttpServletRequest request, HttpServletResponse response)

  21. throws ServletException, IOException {

  22. doPost(request,response);

  23. }

  24. /**

  25. * The doPost method of the servlet. <br />

  26. *

  27. * This method is called when a form has its tag value method equals to post.

  28. *

  29. * @param request the request send by the client to the server

  30. * @param response the response send by the server to the client

  31. * @throws ServletException if an error occurred

  32. * @throws IOException if an error occurred

  33. */

  34. public void doPost(HttpServletRequest request, HttpServletResponse response)

  35. throws ServletException, IOException {

  36. //String user = UUID.randomUUID().toString().replace("-", "");

  37. //request.getSession().setAttribute("user", user);

  38. SaeChannel channel = new SaeChannel();

  39. String user="bingobing";

  40. String name = user;//channel名稱(若是是一對一的推送服務,那麼每一個用戶的CHANNEL名稱應該是不一樣的,當推送消息時就是依據這個名字)

  41. int duration = 1000;//channel過時時間(單位爲秒)

  42. String url = channel.createChannel(name, duration);//返回值爲WebSocket的url

  43. response.sendRedirect("index.jsp?url="+url);

  44. }

  45. }

二、在INDEX.JSP頁面中根據以前產生的CHANEL服務URL將JS客戶端鏈接上CHANEL服務

Java代碼 

  1. < %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  2. < !DOCTYPE HTML>

  3. <html>

  4.   <head>

  5.   <script type="text/javascript" src="http://channel.sinaapp.com/api.js"></script>

  6.   <script type="text/javascript">

  7.   var socket = new sae.Channel('< %=request.getParameter("url") %>');//此處url爲上面建立channel返回的url

  8.   socket.onopen = function(){

  9.   alert("open socket!");

  10.   }

  11.   //設置channel打開事件,

  12.   socket.onmessage = function(message){//設置接收CHANNEL服務推送消息事件

  13.   //其中message對象的data字段爲channel發送的消息,如上範例爲"this message"

  14.   alert(message.data);

  15.   }

  16.   </script>

  17.   </head>

  18.   <body>

  19.   </body>

  20. </html>

三、經過服務端向指定的CHANNEL客戶端推送消息

Java代碼 

  1. import java.io.IOException;

  2. import java.io.PrintWriter;

  3. import javax.servlet.ServletException;

  4. import javax.servlet.http.HttpServlet;

  5. import javax.servlet.http.HttpServletRequest;

  6. import javax.servlet.http.HttpServletResponse;

  7. import com.sina.sae.channel.SaeChannel;

  8. public class ChanelService extends HttpServlet {

  9. /**

  10. * The doGet method of the servlet. <br />

  11. *

  12. * This method is called when a form has its tag value method equals to get.

  13. *

  14. * @param request the request send by the client to the server

  15. * @param response the response send by the server to the client

  16. * @throws ServletException if an error occurred

  17. * @throws IOException if an error occurred

  18. */

  19. public void doGet(HttpServletRequest request, HttpServletResponse response)

  20. throws ServletException, IOException {

  21. doPost(request,response);

  22. }

  23. /**

  24. * The doPost method of the servlet. <br />

  25. *

  26. * This method is called when a form has its tag value method equals to post.

  27. *

  28. * @param request the request send by the client to the server

  29. * @param response the response send by the server to the client

  30. * @throws ServletException if an error occurred

  31. * @throws IOException if an error occurred

  32. */

  33. SaeChannel channel = new SaeChannel();

  34. public void doPost(HttpServletRequest request, HttpServletResponse response)

  35. throws ServletException, IOException {

  36. //String name=(String) request.getSession().getAttribute("user");

  37. String name="bingobing";

  38. int nums=channel.sendMessage(name, "aaaaa");//經過指定的CHANNEL服務名稱,向使用該CHANNEL服務的客戶端推送消息

  39. response.getWriter().print(nums);

  40. }

  41. }

當經過網頁訪問ChanelService  時,全部使用BINGOBING名稱的CHANNEL客戶端都會收到一條內容爲AAAAA的消息。客戶端並無進行任何操做,徹底由服務器主動推送至客戶端。

作了個簡單的DEMO而已,具體的應用尚未想好,不過實現方式真的很簡單,這裏就想到了以前爲一個朋友作的微信公衆平臺的一個在線客服功能模塊,客戶要求,當微信公衆平臺關注用戶向微信公衆號發送指定規則拼接的客服請求時,客服人員能經過自行開發管理平臺的後臺客服處理界面,能實時接收到用戶的客服請求內容,並即時向客戶進行返回,當時的實現方式是經過AJAX輪詢的方式,來實現實時性要求,不過如今有了CHANNEL服務,就很容易實現了。

當客服人員登陸管理平臺的客服處理界面後,建立一個指定名稱的CHANNEL服務,並連接上此CHANNEL服務。當微信公衆號關注分析提出在線客服請求後,將客服請求內容經過調用CHANNEL服務,向指定名稱的服務發送消息,這樣客服就能實時獲取到粉絲的請求,並進行反饋,這樣的好處在於一、避免了AJAX輪詢致使的反覆請求服務器,減小了服務器資源的開銷;二、AJAX輪詢的請求頻率有必定的延遲,沒法作到實時,只能作到一個很高頻率的循環而已,用了CHANNEL服務,能實現實時性要求。

相關文章
相關標籤/搜索