[英]Getting error 404 when trying to reach a websocket
I am trying to set a simple example for a web socket in my site but I have no luck despite I reviewed a lot of similar cases and tutorials. 我正在尝试为我的站点中的Web套接字设置一个简单的示例,但是尽管我阅读了很多类似的案例和教程,但我还是没有运气。
This is my config at server 这是我在服务器上的配置
java version "1.8.0_191"
Java版本“ 1.8.0_191”
Server version: Apache Tomcat/7.0.52 (Ubuntu)服务器版本:Apache Tomcat / 7.0.52(Ubuntu)
And I use Netbeans at local. 我在本地使用Netbeans。
Server side 服务器端
This is my class. 这是我的课。 I am not registering anything at web.xml.
我没有在web.xml中注册任何东西。
package com.myserver.server.monitor;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
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(value = "/wss")
public class MonitorWebSocket {
private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen (Session peer) {
peers.add(peer);
}
@OnClose
public void onClose (Session peer) {
peers.remove(peer);
}
@OnMessage
public String mensaje(String mensaje) {
return "Hi, from server. The message was:" +mensaje;
}
@OnError
public void onError(Throwable t) {
}
}
Client side: 客户端:
Before you point it, I think the URI is ok, as the context is ROOT. 在您指出它之前,我认为URI可以,因为上下文是ROOT。
I am typing 'wss' as it is a htpps site, and I get a coherent security exception if I use 'ws' 我正在输入“ wss”,因为它是一个htpps网站,如果使用“ ws”,则会收到一致的安全异常
function initPerformance(){
var uriWS="wss://myserver.com/wss";
var miWebsocket= new WebSocket(uriWS);
console.log (miWebsocket);
miWebsocket.onopen=function(evento) {
console.log("open");
miWebsocket.send("hi");
};
miWebsocket.onmessage=function(evento) {
console.log(evento.data);
};
}
Results 结果
in chrome console (similar results on FF): 在Chrome控制台中(FF上的类似结果):
performance.js:4 WebSocket connection to 'wss://myserver.com/wss' failed: Error during WebSocket handshake: Unexpected response code: 404
performance.js:4与'wss://myserver.com/wss'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:404
I wonder if some miss match on libraries between my development environment and server could be the reason, but catalina.out is not complaining about any import. 我想知道我的开发环境和服务器之间的库是否缺少匹配可能是原因,但是catalina.out没有抱怨任何导入。
Placing ' https://myserver.com/wss ' on browser's bar also gives a 404 error 将' https://myserver.com/wss '放置在浏览器栏上也会产生404错误
Any help will be appreciated. 任何帮助将不胜感激。
Edit: 编辑:
I found this valuable resource: 我发现了这一宝贵资源:
wss://echo.websocket.org
WSS://echo.websocket.org
which I could try to call from client side. 我可以尝试从客户端致电。 As I expected it works, so my problem is probably on server side.
正如我预期的那样,所以我的问题可能在服务器端。
Not sure what my therapist will think about talking alone here, but the thing is I came to a solution. 不知道我的治疗师会如何考虑在这里单独谈话,但问题是我找到了解决方案。 Nothing in the above path worked and seems is not a good way for Tomcat7.
上面的路径没有任何作用,对于Tomcat7来说似乎不是一个好方法。 I try before to implement it with a
WebSocketServlet
that seems to be the good old way for Tomcat7. 我尝试过使用
WebSocketServlet
来实现它,这似乎是Tomcat7的旧方法。 But I had problems finding the correct libraries. 但是我在找到正确的库时遇到了问题。 At the end this is working for me:
最后这对我有用:
Libraries (JAR files) 库(JAR文件)
This one lets me invoke the 'old stuff' for 'WebSocketServlet' and other related classes. 这使我可以调用“ WebSocketServlet”和其他相关类的“旧东西”。
I was not done, as everything seemed ok to compile in netbeans, but this error occurred: 我没有做完,因为一切似乎都可以在netbeans中编译,但是发生了此错误:
class file for org.apache.coyote.http11.upgrade.UpgradeInbound not found
找不到org.apache.coyote.http11.upgrade.UpgradeInbound的类文件
But this jar got rid of it ( as described here ). 但是这个罐子摆脱了它( 如此处所述 )。
Server side 服务器端
Restructuring the code, I have two classes: 重组代码,我有两个类:
A Servlet like this: 像这样的Servlet:
package com.myserver.server.monitor;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
@WebServlet("/wsocket")
public class MonitorServlet extends WebSocketServlet {
private static final long serialVersionUID = 1L;
// for new clients, <sessionId, streamInBound>
private static ConcurrentHashMap<String, StreamInbound> clients = new ConcurrentHashMap<String, StreamInbound>();
@Override
public void init(){
}
@Override
protected StreamInbound createWebSocketInbound(String protocol,HttpServletRequest httpServletRequest) {
// Check if exists
HttpSession session = httpServletRequest.getSession();
// find client
StreamInbound client = clients.get(session.getId());
if (null != client) {
return client;
} else {
client = new MyInBound(httpServletRequest,this);
clients.put(session.getId(), client);
}
return client;
}
}
And the class MyInBound, like this: 和类MyInBound一样,如下所示:
package com.myserver.server.monitor;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
public class MyInBound extends MessageInbound{
//private String name;
private WsOutbound myoutbound;
private final WebSocketServlet servlet;
public MyInBound(HttpServletRequest httpServletRequest, WebSocketServlet servlet) {
this.servlet=servlet;
}
@Override
public void onOpen(WsOutbound outbound) {
this.myoutbound = outbound;
System.out.println("Hey!!---------");
}
@Override
public void onClose(int status) {
System.out.println("Close client");
}
@Override
protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
}
@Override
protected void onTextMessage(CharBuffer inChar) throws IOException {
}
}
Now you should register the servlet in web.xml: 现在,您应该在web.xml中注册servlet:
<servlet>
<servlet-name>MonitorServlet</servlet-name>
<servlet-class>com.myserver.server.monitor.MonitorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MonitorServlet</servlet-name>
<url-pattern>/MonitorServlet</url-pattern>
</servlet-mapping>
Client side 客户端
It didn't change very much: 它并没有太大变化:
function initPerformance(){ 函数initPerformance(){
var uriWS="wss://myserver.com/wsocket";
var miWebsocket= new WebSocket(uriWS);
console.log (miWebsocket);
miWebsocket.onopen=function(evento) {
console.log("open");
miWebsocket.send("hi");
};
miWebsocket.onmessage=function(evento) {
console.log(evento.data);
};
} }
Hope it helps someone! 希望它能对某人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.