Java Tomcat websocket不工作
我尝试了下面的websocket示例代码,我的浏览器支持HTML5WebSocket,但下面的示例代码在javascript中总是提示“关闭”。代码怎么了 websocket.javaJava Tomcat websocket不工作,java,javascript,tomcat,websocket,Java,Javascript,Tomcat,Websocket,我尝试了下面的websocket示例代码,我的浏览器支持HTML5WebSocket,但下面的示例代码在javascript中总是提示“关闭”。代码怎么了 websocket.java @WebServlet("/websocket") public class websocket extends WebSocketServlet { private static final long serialVersionUID = 1L; protected
@WebServlet("/websocket")
public class websocket extends WebSocketServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("welcome to websocket 2");
response.getWriter().flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
@Override
protected StreamInbound createWebSocketInbound(String arg0,
HttpServletRequest arg1) {
return new TheWebSocket();
}
private class TheWebSocket extends MessageInbound
{
private WsOutbound outbound;
@Override
public void onOpen( WsOutbound outbound )
{
this.outbound = outbound;
System.out.println("socket opened!");
}
@Override
public void onTextMessage( CharBuffer buffer ) throws IOException
{
try
{
outbound.writeTextMessage( CharBuffer.wrap( "abc testing".toCharArray() ) );
System.out.println("Message sent from server.");
}
catch ( IOException ioException )
{
System.out.println("error opening websocket");
}
}
@Override
protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
// TODO Auto-generated method stub
}
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
<script type="text/javascript">
var ws = null;
function startWebSocket() {
if ('WebSocket' in window)
ws = new WebSocket("ws://localhost:8080/web_test/websocket");
else if ('MozWebSocket' in window)
ws = new MozWebSocket("ws://localhost:8080/web_test/websocket");
else
alert("not support");
ws.onmessage = function(evt) {
alert(evt.data);
};
ws.onclose = function(evt) {
alert("close");
};
ws.onopen = function(evt) {
alert("open");
};
}
function sendMsg() {
ws.send(document.getElementById('writeMsg').value);
}
</script>
</head>
<body onload="startWebSocket();">
<input type="text" id="writeMsg"></input>
<input type="button" value="send" onclick="sendMsg()"></input>
</body>
</html>
指数
var-ws=null;
函数startWebSocket(){
如果('WebSocket'在窗口中)
ws=newwebsocket(“ws://localhost:8080/web_test/WebSocket”);
else if(窗口中的“MozWebSocket”)
ws=newmozwebsocket(“ws://localhost:8080/web_test/websocket”);
其他的
警惕(“不支持”);
ws.onmessage=函数(evt){
警报(evt.数据);
};
ws.onclose=函数(evt){
警报(“关闭”);
};
ws.onopen=函数(evt){
警报(“开放”);
};
}
函数sendMsg(){
send(document.getElementById('writeMsg').value);
}
当我连接到“http://localhost:8080/web_test/websocket,我得到了正确的信息,那就是“欢迎来到websocket 2”。web_测试后,我的index.jsp文件位于根目录中。所以,我的部署应该是好的,但有些地方是错的。我就是想不出来。从servlet代码中注释或删除这两个方法,然后尝试web套接字正常工作。如果servlet中存在这两个方法,则websocket将处于关闭状态
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("welcome to websocket 2");
response.getWriter().flush();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
天哪,真管用。但请原谅我的好奇,当doGet/doPost存在时,它为什么不起作用?我认为WebSocketServlet应该能够处理/区分http并升级websocket?我观察到,当我使用浏览器访问此url时,会显示onclose警报。根据HTTP协议,请求将在处理后关闭。但是websocket是TCP连接,不应该关闭它。不确定内部实现我正在使用ApacheTomcat7和Chrome建立WebSocket。但我最终得到了错误“意外响应代码:200”。有什么想法吗?您使用的是哪台服务器建立握手?@gviswana也许您可以发布您的代码,我也使用Tomcat 7,因为只有Tomcat 7(较低版本)支持websocket。我上面的代码应该可以正常工作,加上下面的答案。您应该能够运行websocket。我正在使用Tomcat 7.0.27。这个版本会支持websocket吗?@gviswanathan我认为这个版本应该支持(我使用的版本是7.0.27),很容易知道,如果它不支持,编译将不会成功。是的,WebSocketServlet在7.0.27中不可用