I have tried a websocket sample code as below, my browser is supporting HTML 5 websocket, but the sample code below always prompt "Close" in the javascript. What happen to the code?
websocket.java
@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>
When I connect to "http://localhost:8080/web_test/websocket", I got correct message which is "welcome to websocket 2". And my index.jsp file is in the root directory after web_test. So, my deployment should be fine, but somewhere is wrong. I just cannot figure it out.
Comment or remove these two methods from your servlet code and then try web sockets working fine.If these two present in the servlet , websocket is going to close state
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With