由于项目有保持实时连接请求数据的需要,通过查资料发现HTTP协议中的长连接可以通过WebSocket来实现,经过一天一夜的研究终于在tomcat上面成功部署了一套简单的webSocket服务,并通过Java端和hmtl端成功访问。
websocket通过发送HTTP包来进行握手连接后,建立TCP长连接,以后的通讯全部基于此链接即可。
通过研读Oracle公司关于websocket的java标准(JSR-356),发现其有自动注入的websocket server的方式代码如下:
package bmw.websocket.controller;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/echo")
public class EchoEndpoint {
@OnOpen
public void open(Session session) {
System.out.println("open");
}
@OnMessage
public void onMessage(Session session, String msg) {
System.out.println("onMessage:" + msg);
try {
session.getBasicRemote().sendText(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void closedConnection(Session session) {
/* Remove this connection from the queue */
System.out.println(session.getAsyncRemote().toString() + " OnClose");
}
@OnError
public void error(Session session, Throwable t) {
// t.printStackTrace();
System.out.println(session.getAsyncRemote().toString() + " error");
}
}
配置信息:
WebSocketTest-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自动扫描 bmw.websocket.controller包下的注解类 -->
<context:component-scan base-package="bmw.websocket.controller" />
<!-- 注解探测器 -->
<!-- <mvc:annotation-driven />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="iie.cas.controler.AccessStatisticsIntceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 启用注解方式 -->
<context:annotation-config />
<!-- 启用mvc注解 -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />
<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
<!-- p:prefix="/WEB-INF/jsp/" -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/"
p:suffix=".jsp" />
</beans>
web.xml:
<!-- 拦截器配置 -->
<servlet>
<servlet-name>WebSocketTest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebSocketTest</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>/**</url-pattern>
</servlet-mapping>
客户端java代码:
package bmw.websocket.controller;
import java.net.URI;
import java.net.URISyntaxException;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
public class MyEndpoint {
public void test(){
try {
WebSocketClient client = new WebSocketClient(new URI("ws://localhost:8080/WebSocketTest/echo")) {
@Override
public void onOpen(ServerHandshake handshakedata) {
// TODO Auto-generated method stub
System.out.println("here1");
}
@Override
public void onMessage(String message) {
// TODO Auto-generated method stub
System.out.println("here2:" + message);
}
@Override
public void onError(Exception ex) {
// TODO Auto-generated method stub
System.out.println("here3");
}
@Override
public void onClose(int code, String reason, boolean remote) {
// TODO Auto-generated method stub
System.out.println("here4");
}
};
client.connect();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(client.getConnection().isOpen()){
client.send("test");
}else {
System.out.println("can't connect");
}
// client.close();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
MyEndpoint endpoint = new MyEndpoint();
endpoint.test();
}
}