java-websocket的搭建很是之容易,沒用框架的童鞋能夠在這裏下載擼主親自調教好的java-websocket程序:javascript
Apach Tomcat 8.0.3+MyEclipse+maven+JDK1.7:css
http://download.csdn.net/detail/up19910522/7719087html
spring4.0之後加入了對websocket技術的支持,在SSM(springMVC+spring+MyBatis)框架中集成websocket技術的曲折蛋疼直至成功喜悅之路。java
- 1 在maven的pom.xml中加入websocket所依賴的jar包,什麼,你不知道maven,快去度之或者查看擼主關於maven的博文惡補一下,spring-websocket所依賴的jar包有如下幾個:
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.3.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.3.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-websocket</artifactId>
- <version>4.0.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-messaging</artifactId>
- <version>4.0.1.RELEASE</version>
- </dependency>
- 2 更新web.xml中namespace.xsd的版本,
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:websocket="http://www.springframework.org/schema/websocket"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
- 3 更新spring框架的jar包至4.0以上(spring-core, spring-context, spring-web and spring-webmvc)
- <dependency>
- <span style="white-space:pre"> </span><groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- package com.up.websocket.handler;
-
- import org.springframework.web.socket.TextMessage;
- import org.springframework.web.socket.WebSocketSession;
- import org.springframework.web.socket.handler.TextWebSocketHandler;
-
- public class WebsocketEndPoint extends TextWebSocketHandler {
-
- @Override
- protected void handleTextMessage(WebSocketSession session,
- TextMessage message) throws Exception {
- super.handleTextMessage(session, message);
- TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");
- session.sendMessage(returnMessage);
- }
- }
- package com.up.websocket;
-
- import java.util.Map;
-
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.web.socket.WebSocketHandler;
- import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
-
- public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{
-
- @Override
- public boolean beforeHandshake(ServerHttpRequest request,
- ServerHttpResponse response, WebSocketHandler wsHandler,
- Map<String, Object> attributes) throws Exception {
- System.out.println("Before Handshake");
- return super.beforeHandshake(request, response, wsHandler, attributes);
- }
-
- @Override
- public void afterHandshake(ServerHttpRequest request,
- ServerHttpResponse response, WebSocketHandler wsHandler,
- Exception ex) {
- System.out.println("After Handshake");
- super.afterHandshake(request, response, wsHandler, ex);
- }
-
- }
- 5 處理類和握手協議的spring配置(applicationContext.xml文件)
- <bean id="websocket" class="com.up.websocket.handler.WebsocketEndPoint"/>
-
- <websocket:handlers>
- <websocket:mapping path="/websocket" handler="websocket"/>
- <websocket:handshake-interceptors>
- <bean class="com.up.websocket.HandshakeInterceptor"/>
- </websocket:handshake-interceptors>
- </websocket:handlers>