简单介绍一下tomcat的webSocketAPI使用:

在这里啰嗦几句:

很多朋友听说webSocket不知道是什么。知道是什么不知道怎么用,知道怎么用不知道具体实现。其实我当初也是这样。

实际上webSocket可以简单的理解为用浏览器与服务器简历socket连接,但是用了一个特殊的协议,偶收协议,它与http协议发送的报头不一样。

websocket需要服务器和浏览器支持,浏览器不支持,也就无法使用这个技术。服务器可以自己实现协议连接,但是我们不准备自己实现(其实看需求,至少对我来说不需要),当然目前javaEE官方不支持这个实现,没有规范(据说jsr356准备支持,期待来年【2013】javaEE7吧)

目前实现的java服务端第三方webSocketAPI不算少,比如jetty就是一种(多的我也举例不了,我只知道,没研究过有多少实现。)tomcat也自带了实现API

webSocket想要手动实现比较麻烦,可以看下tomcat实现过程,大致都一样。

总之一句话,webSocket是一种客户端与服务端连接socket的技术,实现即时消息,取代comet但是并没广泛只用,因为大多需要浏览器的支持,相对comet有很多优点,此处不举例说明。可以自己google一下。

tomcat7.027如何实现webSocket程序:

总的来说,实现webSocket的servlet要继承WebSocketServlet这个类。这个类是tomcat自己包装的servlet。

所有的入口都在protected StreamInbound createWebSocketInbound(String subProtocol) {}这个方法。 也就是说,我们实现这个方法,就可以实现握手协议了。

注意看这个方法。 要求返回StreamInbound类型。这个类型我们需要继承自己实现。打开源码观看这个类

有如下方法

  1. /**
  2. * Intended to be overridden by sub-classes that wish to be notified
  3. * when the outbound connection is established. The default implementation
  4. * is a NO-OP.
  5. *
  6. * @param outbound    The outbound WebSocket connection.
  7. */
  8. protected void onOpen(WsOutbound outbound) {
  9. // NO-OP
  10. }
  11. /**
  12. * Intended to be overridden by sub-classes that wish to be notified
  13. * when the outbound connection is closed. The default implementation
  14. * is a NO-OP.
  15. *
  16. * @param status    The status code of the close reason.
  17. */
  18. protected void onClose(int status) {
  19. // NO-OP
  20. }
  21. /**
  22. * This method is called when there is a binary WebSocket message available
  23. * to process. The message is presented via a stream and may be formed from
  24. * one or more frames. The number of frames used to transmit the message is
  25. * not made visible to the application.
  26. *
  27. * @param is    The WebSocket message
  28. *
  29. * @throws IOException  If a problem occurs processing the message. Any
  30. *                      exception will trigger the closing of the WebSocket
  31. *                      connection.
  32. */
  33. protected abstract void onBinaryData(InputStream is) throws IOException;
  34. /**
  35. * This method is called when there is a textual WebSocket message available
  36. * to process. The message is presented via a reader and may be formed from
  37. * one or more frames. The number of frames used to transmit the message is
  38. * not made visible to the application.
  39. *
  40. * @param r     The WebSocket message
  41. *
  42. * @throws IOException  If a problem occurs processing the message. Any
  43. *                      exception will trigger the closing of the WebSocket
  44. *                      connection.
  45. */
  46. protected abstract void onTextData(Reader r) throws IOException;

上面的方法都是要我们自己实现的。tomcat没有给我们实现。

仔细看都是onXxx格式,类似事件监听。其实也差不多。只是tomcat在得到消息或者链接发生变化的时候会去调用这些方法,实现方法“自动”触发。

仔细看源码还有很多函数可以使用,这里不一一介绍。感兴趣可以打开源码看看。

其实仔细看官方的例子,chat那个例子也能得到这个结论(tomcat的webSocket例子需要tomcat7.027才带有)

我们定义一个servlet

  1. @WebServlet(urlPatterns = { "/chatWebSocket" })
  2. public class ChatWebSocketServlet extends WebSocketServlet {
  3. private static final long serialVersionUID = 1L;
  4. OnLineUser theUser;
  5. @Override
  6. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  7. throws ServletException, IOException {
  8. theUser = (OnLineUser) req.getSession().getAttribute("loginUser");
  9. super.doGet(req, resp);
  10. }
  11. @Override
  12. protected StreamInbound createWebSocketInbound(String subProtocol) {
  13. return new ChatMessageInbound(theUser);
  14. }
  15. }

doget不用说,是连接的开始,然后取出登录的用户,这个是为了管理连接使用的,你在看这个例子的时候不需要doget方法和theUser声明,只要有createWebSocketInbound方法就行。上面说了。这个方法是webSocket的入口。其实也是WebSocketServlet这个类写好的doget,我们看WebSocketServlet的doget是如何写的

  1. @Override
  2. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  3. throws ServletException, IOException {
  4. // Information required to send the server handshake message
  5. String key;
  6. String subProtocol = null;
  7. List<String> extensions = Collections.emptyList();
  8. if (!headerContainsToken(req, "upgrade", "websocket")) {
  9. resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
  10. return;
  11. }
  12. if (!headerContainsToken(req, "connection", "upgrade")) {
  13. resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
  14. return;
  15. }
  16. if (!headerContainsToken(req, "sec-websocket-version", "13")) {
  17. );
  18. resp.setHeader("Sec-WebSocket-Version", "13");
  19. return;
  20. }
  21. key = req.getHeader("Sec-WebSocket-Key");
  22. if (key == null) {
  23. resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
  24. return;
  25. }
  26. String origin = req.getHeader("Origin");
  27. if (!verifyOrigin(origin)) {
  28. resp.sendError(HttpServletResponse.SC_FORBIDDEN);
  29. return;
  30. }
  31. List<String> subProtocols = getTokensFromHeader(req,
  32. "Sec-WebSocket-Protocol-Client");
  33. if (!subProtocols.isEmpty()) {
  34. subProtocol = selectSubProtocol(subProtocols);
  35. }
  36. // TODO Read client handshake - Sec-WebSocket-Extensions
  37. // TODO Extensions require the ability to specify something (API TBD)
  38. //      that can be passed to the Tomcat internals and process extension
  39. //      data present when the frame is fragmented.
  40. // If we got this far, all is good. Accept the connection.
  41. resp.setHeader("upgrade", "websocket");
  42. resp.setHeader("connection", "upgrade");
  43. resp.setHeader("Sec-WebSocket-Accept", getWebSocketAccept(key));
  44. if (subProtocol != null) {
  45. resp.setHeader("Sec-WebSocket-Protocol", subProtocol);
  46. }
  47. if (!extensions.isEmpty()) {
  48. // TODO
  49. }
  50. // Small hack until the Servlet API provides a way to do this.
  51. StreamInbound inbound = createWebSocketInbound(subProtocol);
  52. ((RequestFacade) req).doUpgrade(inbound);
  53. }

注意倒数第三行,调用了createWebSocketInbound方法,我们重写这个方法。

  1. @Override
  2. protected StreamInbound createWebSocketInbound(String subProtocol) {
  3. return new ChatMessageInbound(theUser);
  4. }

上面的ChatMessageInbound是我自己定义的继承类。

  1. public final class ChatMessageInbound extends MessageInbound {
  2. public ChatMessageInbound(OnLineUser theUser) {
  3. this.theUser = theUser;
  4. }
  5. @Override
  6. protected void onOpen(WsOutbound outbound) {
  7. // 添加链接到容器
  8. ChatMessageInbound theBound = this;
  9. ChatContainer.addInbound(theBound.theUser, theBound);
  10. // 向每个在线用户发送消息
  11. ChatContainer.eachAllBound(new ContainerCallBack() {
  12. @Override
  13. public void eachCallBack(ChatMessageInbound theBound, OnLineUser theUser) {
  14. ListUserMsg listUserMsg = new ListUserMsg(ChatContainer.getUserList());
  15. WriteTookit.writeToBound(theBound, listUserMsg.toMsg());
  16. }
  17. });
  18. }
  19. @Override
  20. protected void onClose(int status) {
  21. ChatContainer.removeInbound(theUser);
  22. }
  23. @Override
  24. protected void onBinaryMessage(ByteBuffer message) throws IOException {
  25. }
  26. @Override
  27. protected void onTextMessage(CharBuffer message) throws IOException {
  28. //      CHAT_MODEL.setMessage(message.toString());
  29. //      ChatContainer.eachAllBound(new ContainerCallBack() {
  30. //          @Override
  31. //          public void eachCallBack(ChatMessageInbound theBound, OnLineUser theUser) {
  32. //              WriteTookit.writeToBound(theBound, CHAT_MODEL.getSayMsg());
  33. //          }
  34. //      });
  35. }
  36. // 变量区域
  37. private OnLineUser theUser;
  38. }

这里只是简单实现了一下,注释部分只是处理这个方法的部分,那里是一个容器,存档所有在线用户。并且提供遍历插入以及删除等方法,在自己实现的时候完全不需要这么写。

下面是容器代码

  1. public final class ChatContainer {
  2. /**
  3. * 保存服务器连接的用户的容器
  4. */
  5. private static final Map<OnLineUser, ChatMessageInbound> CHAT_MAP = new HashMap<OnLineUser, ChatMessageInbound>();
  6. /**
  7. * 取出用户的连接
  8. */
  9. public static ChatMessageInbound getInbound(OnLineUser theUser) {
  10. return CHAT_MAP.get(theUser);
  11. }
  12. /**
  13. * 放入一个连接
  14. */
  15. public static void addInbound(OnLineUser theUser,
  16. ChatMessageInbound outbound) {
  17. CHAT_MAP.put(theUser, outbound);
  18. System.out.println(CHAT_MAP.size());
  19. }
  20. /**
  21. * 移除一个连接
  22. *
  23. * @param theUser
  24. * @return
  25. */
  26. public static ChatMessageInbound removeInbound(OnLineUser theUser) {
  27. return CHAT_MAP.remove(theUser);
  28. }
  29. /**
  30. * 遍历所有连接
  31. */
  32. public static void eachAllBound(ContainerCallBack callBackInter) {
  33. Iterator<OnLineUser> keyIter = CHAT_MAP.keySet().iterator();
  34. while (keyIter.hasNext()) {
  35. OnLineUser theUser = keyIter.next();
  36. callBackInter.eachCallBack(CHAT_MAP.get(theUser), theUser);
  37. }
  38. }
  39. /**
  40. * 回调函数的接口
  41. *
  42. * @author WangZhenChong
  43. */
  44. public interface ContainerCallBack {
  45. void eachCallBack(ChatMessageInbound theBound, OnLineUser theUser);
  46. }
  47. }

我定义了一种数据交约定,使用json 字符串,MsgType表示消息类型,类似windows的消息机制

  1. /**
  2. * 前台和后台交互的信息类型常量
  3. *
  4. * @author WangZhenChong
  5. *
  6. */
  7. public final class MsgTypeConstants {
  8. ;// 在线所有用户信息交互
  9. ;// 对一个用户发送消息
  10. ;// 对所有用户发送消息
  11. ;// 发送系统消息
  12. }

余下的msgContent就是消息内容,比如列出现在用户这个内容就是[...,...,...,...]发送消息就是消息模型的内容。

这样解决单通道多操作的方法。

下面列出前台js核心内容。

使用jquery

  1. $(document).ready(function() {
  2. $("#connBtn").bind('click', function() {
  3. $.ajax({
  4. url : "/tomcatWebSocket/Login#?asdasdasd",
  5. type : "POST",
  6. processData : false,
  7. data : $.param({
  8. username : document.getElementById("usernameField").value
  9. }),
  10. success : function(msg, status) {
  11. initChat();
  12. initUserList();
  13. $("#sendBtn").removeAttr("disabled");
  14. $("#connBtn").attr("disabled", "disabled");
  15. $("#usernameField").attr("disabled", "disabled");
  16. },
  17. error : function(jqXHR, textStatus, errorThrown) {
  18. alert("服务器内部错误");
  19. }
  20. });
  21. });
  22. var Chat = {};
  23. Chat.socket = null;
  24. function initChat() {
  25. var wsURL = 'ws://' + window.location.host
  26. + '/tomcatWebSocket/chatWebSocket';
  27. if ('WebSocket' in window) {
  28. Chat.socket = new WebSocket(wsURL);
  29. } else if ('MozWebSocket' in window) {
  30. Chat.socket = new MozWebSocket(wsURL);
  31. } else {
  32. alert("浏览器不支持");
  33. return false;
  34. }
  35. Chat.socket.onopen = function() {
  36. };
  37. Chat.socket.onclose = function() {
  38. Chat.writeToConsole("断开连接了 ");
  39. initChat();
  40. };
  41. Chat.socket.onmessage = function(message) {
  42. if (typeof message.data == "string") {// 如果发送的是字符串信息.
  43. var msgObj = eval("(" + message.data + ")");
  44. switch (msgObj.MsgType) {
  45. case MsgTypeConstants.GET_USER_LIST :// 所有用户信息
  46. Chat.preUserList(msgObj.userList);
  47. break;
  48. case MsgTypeConstants.SEND_ONE_TO_ALL :
  49. Chat.writeToConsole(msgObj.msgContext);
  50. break;
  51. default :
  52. alert("未知错误,请刷新页面");
  53. }
  54. }
  55. };
  56. Chat.sendMessage = function() {
  57. Chat.socket.send(ueditor.getContentTxt());
  58. };
  59. }
  60. Chat.writeToConsole = function(message) {
  61. <span style="white-space: pre;">    </span>//往控制台打印得到的聊天消息
  62. };
  63. /**
  64. * 处理刷新用户信息的方法。
  65. */
  66. Chat.preUserList = function(userList) {
  67. //用户信息列表
  68. };

这些代码只是参考内容,实际上不可能拷贝下来直接运行,

[置顶] WEBSOKET服务器搭建的更多相关文章

  1. [置顶] SVN服务器搭建和使用

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  2. [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……

    两台主机A.B搭建mysql主从复制关系(A为master,B为slave)后,在slave上执行show slave status,结果中显示Last_IO_Error: error connect ...

  3. Windows下基于http的git服务器搭建-gitstack

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Windows下基于http的git服务器搭建-gitstack     本文地址:http: ...

  4. [置顶] IIS应用程序池多工作进程设置及Session共享

    [置顶] IIS应用程序池多工作进程设置及Session共享   在调优iis的时候,朋友分享给我一个特别棒的设置方法步骤,感谢好朋友的分享. IIS应用程序池多工作进程设置及Session共享 1  ...

  5. Git本地服务器搭建及使用详解

    Git本地服务器搭建及使用 Git是一款免费.开源的分布式版本控制系统.众所周知的Github便是基于Git的开源代码库以及版本控制系统,由于其远程托管服务仅对开源免费,所以搭建本地Git服务器也是个 ...

  6. Linux下dns服务器搭建

    Linux下dns服务器搭建1-环境Red Hat Enterprise Linux Server release 6.7 (Santiago)2-配置本地yum源安装dns相关包yum -y ins ...

  7. [置顶]Win2012R2的一个Bug安装群集后可能引发的软件崩溃问题及相应补丁

    [置顶]Win2012R2的一个Bug安装群集后可能引发的软件崩溃问题及相应补丁 如标题,笔者查阅资料发现微软声称安装故障转角色后就可能发生上述描述问题,但不止于SSMS崩溃.建议使用win2012R ...

  8. IIS6.0服务器搭建网站无法访问解决方法

    IIS6.0服务器搭建网站无法访问解决方法     IIS6.0服务器搭建网站无法访问解决方法很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的, ...

  9. Linux下SVN服务器搭建配置

    Linux下SVN服务器搭建配置 1.SVN服务安装 yum install subversion 2.创建SVN代码仓库 mkdir /data/svn svnadmin create /data/ ...

随机推荐

  1. 对Spring from中日期显示格式化问题

    开始时间 结束时间 保存 取消 想在input中让日期格式显示为HH:ss 但是各种百度没有找到答案 最后Google之 http://stackoverflow.com/questions/1173 ...

  2. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  3. IO库 8.1

    题目:编写函数,接受一个istream&参数,返回值类型也是istream&.此函数须从给定流中读取数据,直到遇到文件结束标志时停止.它将读取的数据打印在标准输出上.完成这些操作后,在 ...

  4. 浏览器 HTTP 缓存原理分析

    转自:http://www.cnblogs.com/tzyy/p/4908165.html 浏览器缓存原理: 1.浏览器第一次访问服务器资源/index.html,在浏览器中没有缓存文件,直接向服务器 ...

  5. Fiddler [Fiddler] Connection to localhost. failed.

    原文地址:http://blog.chinaunix.net/uid-20675015-id-1899931.html 在用Fiddler调试本机的网站时,即访问http://localhost,返回 ...

  6. 利用python进行数据分析之数据加载存储与文件格式

    在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...

  7. win10中的vmware桥接模式异常,不能设置同网段ip

    今天发现centOS的桥接模式不正常了,配置 dhclient居然不成功,没有同个网段的ip,于是各种找原因. 突然灵光一现,前几天更新了win10,会不会神马驱动或者服务没安装? 找了一圈,我勒个去 ...

  8. Java—异常处理总结

    异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制异常痛苦,同一个异常或者错误如果多个地方出 ...

  9. 【基础】常用的机器学习&数据挖掘知识点

    Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...

  10. CCNA实验(9) -- Frame Relay

    帧中继的一些特点:1.中小企业常用的广域网线路2.通信费用较低3.配置较为复杂 1.将Cisco路由器配置为帧中继交换机2.帧中继基本配置.帧中继映射3.在帧中继的链路上运行RIPv24.帧中继的多点 ...