SSM websocket 404问题如何解决?

quanjinzhang 2017-03-19 04:10:03
如题,Struts Sping MyBatis框架,增加WebSocket功能ws请求出现404问题,可能是什么原因导致的?
...全文
830 4 打赏 收藏 转发到动态 举报
写回复
用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); }; }

39,083

社区成员

发帖
与我相关
我的任务
社区描述
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
社区管理员
  • HTML5社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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