网址:http://redstarofsleep.iteye.com/blog/1488639
原帖时间是2012-5-8,自己书写时间是2013年6月21日10:39:06
Java代码
package lhc.websocket; import org.apache.catalina.websocket.StreamInbound; import org.apache.catalina.websocket.WebSocketServlet; public class MyWebSocketServlet extends WebSocketServlet { private static final long serialVersionUID = -7178893327801338294L; @Override protected StreamInbound createWebSocketInbound(String arg0) { System.out.println("##########"); return new MyMessageInbound(); } }
这个Servlet继承自WebSocketServlet,实现createWebSocketInbound方法。该方法返回第二个类的实例。
第二个类,处理每一次具体的WebSocket任务:
package lhc.websocket; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import lhc.init.InitServlet; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.WsOutbound; public class MyMessageInbound extends MessageInbound { @Override protected void onBinaryMessage(ByteBuffer arg0) throws IOException { // TODO Auto-generated method stub } @Override protected void onTextMessage(CharBuffer msg) throws IOException { for (MessageInbound messageInbound : InitServlet.getSocketList()) { CharBuffer buffer = CharBuffer.wrap(msg); WsOutbound outbound = messageInbound.getWsOutbound(); outbound.writeTextMessage(buffer); outbound.flush(); } } @Override protected void onClose(int status) { InitServlet.getSocketList().remove(this); super.onClose(status); } @Override protected void onOpen(WsOutbound outbound) { super.onOpen(outbound); InitServlet.getSocketList().add(this); } }
这个类继承自MessageInbound类,必须实现onBinaryMessage和onTextMessage方法。Jetty中只有一个onMessage方法,而Tomcat细化成了2个方法。
还要一个初始化的Servlet
package lhc.init; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.catalina.websocket.MessageInbound; public class InitServlet extends HttpServlet { private static final long serialVersionUID = -3163557381361759907L; private static List<MessageInbound> socketList; public void init(ServletConfig config) throws ServletException { InitServlet.socketList = new ArrayList<MessageInbound>(); super.init(config); System.out.println("Server start============"); } public static List<MessageInbound> getSocketList() { return InitServlet.socketList; } }
最后,web.xml中进行一下Servlet的配置。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>wsoc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>mywebsocket</servlet-name> <servlet-class>lhc.websocket.MyWebSocketServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mywebsocket</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>initServlet</servlet-name> <servlet-class>lhc.init.InitServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> </web-app>
页面的话,就是标准的HTML5的websocket,这个和服务器是否是Tomcat或者Jetty是无关的:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Index</title> <script type="text/javascript"> var ws = null; function startWebSocket() { if ('WebSocket' in window) ws = new WebSocket("ws://localhost:8080/wsoc/mywebsocket.do"); else if ('MozWebSocket' in window) ws = new MozWebSocket("ws://localhost:8080/wsoc/mywebsocket.do"); else alert("not support"); ws.onmessage = function(evt) { alert(evt.data); }; ws.onclose = function(evt) { alert("close"); }; ws.onopen = function(evt) { alert("open"); }; } function sendMsg() { ws.send(document.getElementById('writeMsg').value); } </script> </head> <body onload="startWebSocket();"> <input type="text" id="writeMsg"></input> <input type="button" value="send" onclick="sendMsg()"></input> </body> </html>
---下面讲一下楼主没有说到的地方:
1.相关jar包,需要去tomcat的lib目录中拷贝catalina.jar,tomcat-api.jar,tomcat-coyote.jar,tomcat-util.jar
2.如果做集群,apache支持websocket比较困难,可以采用mod_python进行尝试,目前我还在尝试中。
3.总的来说,做一个基于websocket的前台推送技术需要tomcat版本支持,还需要java中实现tomcat要求实现的功能,js中写几个方法,同时,浏览器方面,IE不支持,火狐和谷歌没有问题,移动端来说,qq浏览器支持度比较好,但是对于flash文件的前台推送,qq浏览器有一些弊端,总的来说,websocket还在发展中,让我们拭目以待。
2013年6月21日10:38:25
落雨
时间: 2024-09-20 00:03:37