社区
HTML5
帖子详情
SSM websocket 404问题如何解决?
quanjinzhang
2017-03-19 04:10:03
如题,Struts Sping MyBatis框架,增加WebSocket功能ws请求出现404问题,可能是什么原因导致的?
...全文
926
4
打赏
收藏
SSM websocket 404问题如何解决?
如题,Struts Sping MyBatis框架,增加WebSocket功能ws请求出现404问题,可能是什么原因导致的?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
yangjuanli
2018-08-14
打赏
举报
回复
你好,我现在遇到的问题是ws可以正常建立链接,wss一直报404,用https可以访问我的项目,请问知道这是什么问题么?若知道帮忙告知下,谢谢!
csdn客服部
2017-03-19
打赏
举报
回复
quanjinzhang
2017-03-19
打赏
举报
回复
已解决,原因有2个: 1、我使用的是tomcat 7容器,tomcat7本身具有对websocket的实现,会和我在pom.xml中配置的如下内容 <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version>冲突,故需要增加一句 <scope>provided</scope>表示只是编译是使用,这个大家可以参考:http://stackoverflow.com/questions/30500998/websocket-handshake-unexpected-response-code-404 2、struts中对websocket请求进行拦截了,需要配置不被拦截 在struts.properties中配置: struts.action.excludePattern=/wsServer 注意: 1、如果想在struts.xml中配置,需先保证struts.properties文件中没有相同项配置,否则会覆盖struts.xml中相同项的配置内容,如在struts.xml中配置,内容如下: <struts> <constant name="struts.action.excludePattern" value="/wsServer"></constant> 2、这里配置的配置的内容,必须是request.getRequestURI()返回部分的内容,而不能配置类似于^ws://,^wss://.*,这是因为struts进行excludePattern匹配时就是使用request.getRequestURI()来进行匹配的,这个大家可以参考源代码org.apache.struts2.dispatcher.PrepareOperations.isUrlExcluded方法
quanjinzhang
2017-03-19
打赏
举报
回复
//服务端 @ServerEndpoint(value = "/wsServer", configurator = GetHttpSessionConfigurator.class) public class WsServerEndpoint { private static final Logger LOGGER = Logger.getLogger(WsServerEndpoint.class); private static final SimpleDateFormat TIME_FMT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); private static Hashtable<String, Session> sessionMap = new Hashtable<String, Session>(); /** * 接收到连接打开请求的处理方法. * * @param session * @param config */ @OnOpen public void onOpen(Session session, EndpointConfig config) { HttpSession httpSession = WsUtil.getHttpSessionFromCfg(config, "WsServerEndpoint.onClose"); LOGGER.info(new StringBuilder("onOpen,").append(httpSession.getAttribute("name")) .append(",").append(httpSession.getId())); sessionMap.put(session.getId(), session); } /** * 接收到连接关闭请求的处理方法. * * @param session * @param config */ @OnClose public void onClose(Session session, EndpointConfig config) { HttpSession httpSession = WsUtil.getHttpSessionFromCfg(config, "WsServerEndpoint.onClose"); LOGGER.info(new StringBuilder("onClose,").append(httpSession.getAttribute("name")) .append(",").append(httpSession.getId())); sessionMap.remove(session.getId()); } /** * 接收到消息请求的处理方法. * * @param session * @param config * @throws IOException */ @OnMessage public void onMessage(String message, Session session, EndpointConfig config) throws IOException { HttpSession httpSession = WsUtil.getHttpSessionFromCfg(config, "WsServerEndpoint.onMessage"); LOGGER.info(new StringBuilder("onMessage,").append(httpSession.getAttribute("name")) .append(",").append(httpSession.getId())); String name = (String) httpSession.getAttribute("name"); String info = new StringBuilder().append(name).append(" has sent message :").append(message) .toString(); Session nowSess = sessionMap.get(session.getId()); for (Session everySess : session.getOpenSessions()) { if (nowSess == everySess) { // 回复当前发起消息的客户端 everySess.getBasicRemote().sendText( new StringBuilder("server has got your message!").append( TIME_FMT.format(new Date())).toString()); } else { // 发送给其他客户端的消息 everySess.getBasicRemote().sendText(info); } } } @OnError public void onError(Session session, EndpointConfig config, Throwable e) throws IOException { HttpSession httpSession = WsUtil.getHttpSessionFromCfg(config, "WsServerEndpoint.onError"); LOGGER.info(new StringBuilder("onError,").append(httpSession.getAttribute("name")) .append(",").append(httpSession.getId())); session.getBasicRemote().sendText( new StringBuilder("server encounter an exception,").append(TIME_FMT.format(new Date())) .append(",e=").append(e).toString()); } } public class GetHttpSessionConfigurator extends Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { Object httpSession = request.getHttpSession(); sec.getUserProperties().put(HttpSession.class.getName(), httpSession); } } //客户端 var ws = null; function connect() { ws= new WebSocket("ws://localhost:8088/wsServer"); ws.onopen = function () { setConnected(true); }; ws.onmessage = function (event) { log('Received: ' + event.data); }; ws.onclose = function (event) { setConnected(false); log('Info: connection closed.'); log(event); }; }
SSM
+
websocket
实现服务器消息推送
解决
404
问题
最近在用业余时间做一个设备维修管理项目,为了完善用户体验,要在A工程师提交转派工单时或发送请求时,B工程师能收到即时消息且在网页显示,并作出处理。网上很多大拿都推荐使用
websocket
实现消息推送,而我的项目是基于
SSM
开发的,于是学着在当前框架上整合
websocket
。简单一番复制粘贴,却总是提示
404
错误。网上重复的文章太多了,大家都在互相转载,也没几个能直接说明
问题
的。下面把我的
解决
过程记...
WebSocket
项目整合踩坑记录
WebSocket
项目整合踩坑记录前言
WebSocket
connection to 'ws://localhost:8080/ws' failed: Error during
WebSocket
handshake: Unexpected response code:
404
Spring webosket 拦截器中获取不到登录sessionPS 前言 项目中集成webScoket遇到很多莫名其...
WebSocket
连接
404
(一种情况)
第一种情况 原因:这里是因为url的路径写错了,请仔细检查是否路径写错了(楼主这里忘了在端口号后面加上项目发布名了) 第二种情况 上一种情况,我使用的是
SSM
框架,后来想用Spring Boot来尝试一下,结果发现按照原来的写法无法连接。如果你使用的是Spring Boot请检查如下配置 声明将@ServerEndPoint的注解交由Spring管理 package com.yunx...
记一次
解决
前端
websocket
调用后台失败报
404
正常心跳(3s一次): 失败心跳 前端: 后端:
解决
方案: 我用的是idea 2020社区版,所以,是在idea里进行配置的 按快捷键ctrl + shift + alt + s 或者是点击下图中“A”处,打开窗口,将
websocket
-api.jar 的适用范围由Compile改为Provide,ok,
解决
问题
。(如果是maven的话,应该是改jar的scope属性) ...
关于
websocket
的http无法升级到ws请求的错误The HTTP response from the server [
404
] did not permit the HTTP upgrad
错误:javax.
websocket
.DeploymentException: The HTTP response from the server [
404
] did not permit the HTTP upgrade to
WebSocket
环境: springboot +
ssm
+
webSocket
项目有两个服务器一个在公网上,一个在私网上, 私网可以访问公网,但是公网不能访问私网...
HTML5
43,739
社区成员
5,567
社区内容
发帖
与我相关
我的任务
HTML5
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
复制链接
扫一扫
分享
社区描述
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章