SSH框架,Websocket连接失败,求大神指导

zhaojinmeng 2016-03-31 07:03:29
环境:jdk8、tomcat8
使用的是Spring API for WebSocket,项目框架是SSH。
问题:websocket客户端一直连接失败,
报错信息:WebSocket connection to 'ws://192.168.1.110:8080/项目名/collectionList' failed: Error during WebSocket handshake: Unexpected response code: 404。
下面贴上前后台代码,求大神帮忙分析一下
...全文
837 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
晓风吹雾 2016-06-15
  • 打赏
  • 举报
回复
引用 9 楼 s09122289 的回复:
[quote=引用 8 楼 shnulaa 的回复:] The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
貌似不行 这个jar已经在工程中了[/quote] 说错了,是去除工程的jar包 <scope>provided</scope> Try to not export your Websocket-library to your webserver, thus it uses its own implementation. 不要将Websocket-library导入到webserver
s09122289 2016-06-15
  • 打赏
  • 举报
回复
引用 8 楼 shnulaa 的回复:
The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
貌似不行 这个jar已经在工程中了
晓风吹雾 2016-06-15
  • 打赏
  • 举报
回复
The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
s09122289 2016-06-15
  • 打赏
  • 举报
回复
楼主解决了吗 我也遇到类似问题了 能分享下解决思路吗
jollroy 2016-04-01
  • 打赏
  • 举报
回复
引用 3 楼 zhaojinmeng 的回复:
核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); }
这个地址/collectionList 你访问的到吗? 你直接在浏览器里访问这个地址看看
zhaojinmeng 2016-04-01
  • 打赏
  • 举报
回复
引用 4 楼 jollroy 的回复:
[quote=引用 3 楼 zhaojinmeng 的回复:] 核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); }
这个地址/collectionList 你访问的到吗? 你直接在浏览器里访问这个地址看看[/quote] 在浏览器直接访问地址?这又不是http请求
zhaojinmeng 2016-04-01
  • 打赏
  • 举报
回复
前台代码: var websocket; var host=window.location.host; if ('WebSocket' in window) { alert("WebSocket"); websocket = new WebSocket("ws://192.168.1.110:8080/项目名/collectionList"); }else { alert("服务器不支持websocket"); return; } websocket.onopen = function (evnt) { alert("链接服务器成功!") }; websocket.onmessage = function (evnt) { alert("接收到信息:"+evnt.data); }; websocket.onerror = function (evnt) { alert("错误"); }; websocket.onclose = function (evnt) { alert("与服务器断开了链接!") }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
后台WebSocketConfig: @Configuration @EnableWebMvc @EnableWebSocket public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer { /** * 注册websocket */ @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { // 注册多个websocket服务,addHandler第一个参数是websocket的具体业务处理类,第二个参数collectionList相当于endpoint registry.addHandler(RealSocketHandler(), "/collectionList").addInterceptors(new WebSocketHandshakeInterceptor()); // 可以注册多个websocket的endpoint } @Bean public WebSocketHandler RealSocketHandler() { return new RealSocketHandler(); } }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus arg1) throws Exception { // TODO Auto-generated method stub logger.debug("websocket connection closed"); System.out.println("afterConnectionClosed"); clients.remove(session); } //初次连接成功 @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // TODO Auto-generated method stub logger.debug("connect to the websocket succcess ... ..."); System.out.println("afterConnectionEstablished"); clients.add(session); String userName = (String) session.getAttributes().get("session_username"); if(userName!=null){ int count=5; session.sendMessage(new TextMessage(count+"")); } } //接收消息处理信息 @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> arg1) throws Exception { // TODO Auto-generated method stub System.out.println("handleMessage"); sendMessageToUsers(new TextMessage(arg1.getPayload()+"")); } @Override public void handleTransportError(WebSocketSession session, Throwable arg1) throws Exception { // TODO Auto-generated method stub System.out.println("handleTransportError"); logger.debug("websocket connection closed"); if(session.isOpen()){ session.close(); } clients.remove(session); } @Override public boolean supportsPartialMessages() { // TODO Auto-generated method stub return false; } /** * 给所有在线用户发送消息 * * @param message * */ public void sendMessageToUsers(TextMessage message) { for (WebSocketSession client : clients) { try { if (client.isOpen()) { client.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } } /** * 给某个用户发送消息 * * @param userName * @param message */ public void sendMessageToUser(String userName, TextMessage message) { for (WebSocketSession client : clients) { if (client.getAttributes().get("websocket_username").equals(userName)) { try { if (client.isOpen()) { client.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } break; } } } }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
捂手过滤器: public class WebSocketHandshakeInterceptor implements HandshakeInterceptor { @SuppressWarnings("unused") private static Logger logger = LoggerFactory.getLogger(HandshakeInterceptor.class); @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; HttpSession session = servletRequest.getServletRequest().getSession(false); String userName="systest"; if (session != null) { //使用userName区分WebSocketHandler,以便定向发送消息 //String userName = (String) session.getAttribute("session_username"); if(userName != null){ attributes.put("websocket_username",userName); } } } return true; } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { } }

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧