【websocket】sockjs握手请求发送成功,但是没有握手成功
背景
使用spring websocket实现后台给前端推送消息
在支持websocket的浏览器版本上能够正常实现消息的收发,现在使用sockjs做为不支持websocket的浏览器的兼容方案。
问题
浏览器支持websocket时:
并且后端会输出对应的连接成功的日志信息
当浏览器不支持websocket时,使用sockjs:
发现握手请求发送成功了,但是并没有握手成功,连接没有建立起来,也没有相关的日志信息输出。
"/info"和"t="是sockjs自己加上去的,我原来的url是http://localhost:8081/FGMSP-ui/sockjs/websocket?uid=2131231
问题一
为什么sockjs会在我原有的url上自动加上info
的后缀和t
这个参数?
另外sockjs之后还发起过很多次类似的请求,如下图
都是在我原先的url上加了后缀和参数,这是什么原理啊?
问题二
为什么我的第一次的握手请求发送显示成功了(在调试DispatcherServlet类的doDispatch()时也看到了这个请求已经进来),但是没有被interceptor和handler处理?是我的配置出错了吗?
下面是我的一些代码和配置
代码
config
@Configuration
@EnableWebMvc
@EnableWebSocket
public class MySocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
@Autowired
private MySocketHandler mySocketHandler;
@Autowired
private MyInterceptor myInterceptor;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(mySocketHandler, "/websocket")
.addInterceptors(myInterceptor);
// 浏览器不支持websocket,使用sockjs建立连接
registry.addHandler(new MySocketHandler(), "/sockjs/websocket/info")
.addInterceptors(myInterceptor)
.withSockJS();
}
}
interceptor
public class MyInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler webSocketHandler, Map<String, Object> attributes) throws Exception {
// System.out.println("Websocket:用户[ID:" + ((ServletServerHttpRequest) request).getServletRequest().getSession(false).getAttribute("uid") + "]已经建立连接");
if (request instanceof ServletServerHttpRequest) {
// 获取请求参数,首先我们要获取HttpServletRequest对象才能获取请求参数;当ServerHttpRequset的层次结构打开后其子类可以获取到我们想要的http对象,那么就简单了。
// 我这里是把获取的请求数据绑定到session的map对象中(attributes)
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
String id = servletRequest.getSession().getId();
// 标记用户
String uid = servletRequest.getParameter("uid");
attributes.put("uid", uid);
if(uid!=null){
attributes.put("uid", uid);
System.out.println(String.format("websocket建立握手前:sessionId[%s],userId[%s]", id, uid));
}else{
return false;
}
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
System.out.println("握手后");
}
}
handler
public class MySocketHandler implements WebSocketHandler {
private static final Logger logger = Logger.getLogger(MySocketHandler.class);
// websocket连接建立
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
logger.info("Websocket Connection established");
logger.info("getId:" + session.getId());
logger.info ("getLocalAddress:" + session.getLocalAddress().toString());
logger.info ("getUri:" + session.getUri().toString());
session.sendMessage(new TextMessage("Server:connected OK!"));
String uid = (String) session.getHandshakeAttributes().get("uid");
if (SocketSessionMap.socketSession.get(uid) == null) {
SocketSessionMap.socketSession.put(uid, session);
}
}
// 消息处理
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
}
// 消息传输错误
@Override
public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception {
if (session.isOpen()) {
session.close();
}
// 移除Socket会话
Iterator<Map.Entry<String, WebSocketSession>> iter = SocketSessionMap.socketSession.entrySet().iterator();
while (iter.hasNext()) {
ConcurrentHashMap.Entry entry = iter.next();
if (entry.getValue() == session) {
SocketSessionMap.socketSession.remove(entry.getKey());
System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
break;
}
}
}
// websocket 连接关闭
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
// TODO Auto-generated method stub
System.out.println("Websocket:" + session.getId() + "已经关闭");
// 移除Socket会话
Iterator<Map.Entry<String, WebSocketSession>> iter = SocketSessionMap.socketSession.entrySet().iterator();
while (iter.hasNext()) {
ConcurrentHashMap.Entry entry = iter.next();
if (entry.getValue() == session) {
SocketSessionMap.socketSession.remove(entry.getKey());
System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
break;
}
}
}
@Override
public boolean supportsPartialMessages() {
return false;
}
/**
* 给某个用户发送消息
*
* @param uid
* @param message
* @throws IOException
*/
public void sendMessageToUser(String uid, TextMessage message)
throws IOException {
WebSocketSession session = SocketSessionMap.socketSession.get(uid);
if (session != null && session.isOpen()) {
session.sendMessage(message);
}
}
}
配置
DispatcherServlet配置
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/websocket</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/sockjs/websocket/info</url-pattern>
</servlet-mapping>
<servlet>
<bean id="mySocketHandler" class="com.fingard.rh.msp.socket.MySocketHandler"/>
<!-- 握手接口/拦截器 -->
<bean id="myInterceptor" class="com.fingard.rh.msp.socket.MyInterceptor"/>
<websocket:handlers >
<websocket:mapping path="/websocket" handler="mySocketHandler"/>
<websocket:handshake-interceptors>
<ref bean="myInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
<!-- sockJS -->
<websocket:handlers>
<websocket:mapping path="/sockjs/websocket/info" handler="mySocketHandler"/>
<websocket:handshake-interceptors>
<ref bean="myInterceptor"/>
</websocket:handshake-interceptors>
<websocket:sockjs />
</websocket:handlers>
问题一、问题二。请不吝赐教,万分感激!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你好,楼主这个问题解决了么?请问怎么解决的呢?