Chrome,Tomcat7、Chrome下web socket的一个例子 http://tyrion.iteye.com/blog/1868654
WebSocket 实战 https://www.ibm.com/developerworks/cn/java/j-lo-WebSocket/
在spring3.2.0 hibernate3.3.2 struts2 2.2.3中使用websocket,运行环境是tomcat8.0.36,获取HttpSession http://www.itdadao.com/articles/c15a334026p0.html
前台使用 : sockjs.js + jquery, 没有提供,注意send消息必须在open成功之后。
后台代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>7.0.62</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-coyote</artifactId> <version>7.0.62</version> <scope>provided</scope> </dependency> |
1 2 3 4 5 6 7 8 9 |
<servlet> <servlet-name>NotifyServlet</servlet-name> <servlet-class>cn.com.voge.system.servlet.NotifyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>NotifyServlet</servlet-name> <url-pattern>/notify</url-pattern> </servlet-mapping> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package cn.com.voge.system.servlet; import cn.com.voge.actor.security.SessionListener; import org.apache.catalina.websocket.StreamInbound; import org.apache.catalina.websocket.WebSocketServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; /** * 项目名称: idea * 功能说明: TODO: 只适合 Tomcat 7.0.62以上的版本 * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 17-8-8. * 创建时间: 下午1:30. * 修改历史: * ----------------------------------------------- */ public class NotifyServlet extends WebSocketServlet { private static final long serialVersionUID = -328927602003370849L; private Logger logger = LoggerFactory.getLogger(NotifyServlet.class.getName()); @Override /** * 与7.0.27不同的,Tomcat改变了createWebSocketInbound方法的定义,增加了一个HttpServletRequest参数, * 这样我们也可以从request参数中获取更多请求方的信息 * */ protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { NotifyMessageInbound messageInbound= new NotifyMessageInbound(request.getSession()); synchronized (SessionListener.messageInboundMap){ SessionListener.messageInboundMap.put(request.getSession().getId(),messageInbound); } return messageInbound; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package cn.com.voge.system.servlet;/** * Created by pandy on 17-8-8. */ import cn.com.voge.system.service.notify.TaskSystemNotifyService; import cn.com.voge.utils.SpringUtils; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.WsOutbound; import javax.servlet.http.HttpSession; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; /** * 项目名称: idea * 功能说明: * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 17-8-8. * 创建时间: 下午2:58. * 修改历史: * ----------------------------------------------- */ public class NotifyMessageInbound extends MessageInbound { private HttpSession session = null; private WsOutbound outbound; public NotifyMessageInbound(HttpSession session) { this.session = session; } @Override protected void onClose(int status) { } @Override protected void onOpen(WsOutbound outbound) { this.outbound = outbound; try { TaskSystemNotifyService jobTaskSystemNotify = (TaskSystemNotifyService) SpringUtils.getBean("jobTaskSystemNotify"); jobTaskSystemNotify.notify(session.getId(), this.outbound); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onBinaryMessage(ByteBuffer buffer) throws IOException { } // 有文本消息数据到达 @Override protected void onTextMessage(CharBuffer buffer) throws IOException { } public HttpSession getSession() { return session; } public WsOutbound getOutbound() { return outbound; } } |