Tomcat webcam is not working

I tried the websocket sample code as shown below, my browser supports HTML 5 websocket, but the sample code below always asks for "Close" in javascript. What happens 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 received the correct message, which is "welcome to websocket 2". And the index.jsp file is in the root directory after web_test. So, my deployment should be fine, but somewhere wrong. I just can't understand.

+6
source share
1 answer

Comment on or remove these two methods from your servlet code, and then try network sockets that work just fine. If these two are present in the servlet, websocket closes the 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 } 
+6
source

Source: https://habr.com/ru/post/925192/


All Articles