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服务,能实现实时性要求。

相关文章
相关标签/搜索