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中。

下面就是我的程序部分,

1、  INDEXSEVLET,这个大家自定,也可以直接把这部分程序写在INDEX.JSP中。这里就是为用户创建一个CHANEL对象,并产生一个CHANEL服务的URL。

Java代码 

  1. import java.io.IOException;

  2. import java.io.PrintWriter;

  3. import java.util.UUID;

  4. import javax.servlet.ServletException;

  5. import javax.servlet.http.HttpServlet;

  6. import javax.servlet.http.HttpServletRequest;

  7. import javax.servlet.http.HttpServletResponse;

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

  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. }

2、在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>

3、通过服务端向指定的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服务,向指定名称的服务发送消息,这样客服就能实时获取到粉丝的请求,并进行反馈,这样的好处在于1、避免了AJAX轮询导致的反复请求服务器,减少了服务器资源的开销;2、AJAX轮询的请求频率有一定的延迟,无法做到实时,只能做到一个很高频率的循环而已,用了CHANNEL服务,能实现实时性要求。


转载于:https://my.oschina.net/u/1777508/blog/306426

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值