Home > Web Front-end > HTML Tutorial > [websocet] Can the handshake be successful if written like this? _html/css_WEB-ITnose

[websocet] Can the handshake be successful if written like this? _html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:02:29
Original
1067 people have browsed it

package websocket;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.catalina.websocket.WebSocketServlet; import org.apache.catalina.websocket.StreamInbound;public class WebSocketMessageServlet extends WebSocketServlet{	/**	 * 	 */	private static final long serialVersionUID = 1L;	private  String  username;        @Override      protected void doGet(HttpServletRequest req, HttpServletResponse resp)              throws ServletException, IOException {      	this.username = (String)req.getSession().getAttribute("user_nickname");          super.doGet(req, resp);      }     	@Override	protected StreamInbound createWebSocketInbound(String arg0,			HttpServletRequest arg1) {		// TODO Auto-generated method stub		return new WebSocketMessageInboundPool(this.username); 	}	 }  
Copy after login

package websocket;import java.io.IOException;import java.nio.CharBuffer;import java.nio.ByteBuffer;import java.util.HashMap;import java.util.Map;import org.apache.catalina.websocket.MessageInbound;import org.apache.catalina.websocket.WsOutbound;public class WebSocketMessageInboundPool extends MessageInbound{		private final  String user_nickname;	private static final Map<String,WebSocketMessageInboundPool > Connections = new HashMap<String,WebSocketMessageInboundPool>();		public  WebSocketMessageInboundPool(String username){		this.user_nickname=username;	}		protected void onOpen(WsOutbound outbound) {		// TODO Auto-generated method stub		addMessageInbound(this);		super.onOpen(outbound);	}		protected void onBinaryMessage(ByteBuffer arg0) throws IOException {		// TODO Auto-generated method stub		// 向所有已连接的客户端发送文本消息		try {			//向特定的用户发送数据			WebSocketMessageInboundPool  inbound = Connections.get(user_nickname);			if(inbound != null){				ByteBuffer bb = ByteBuffer.wrap(arg0.array());				WsOutbound wb = inbound.getWsOutbound();				wb.writeBinaryMessage(bb);				wb.flush();			}		}		catch (IOException e) {			e.printStackTrace();		}	}		protected void onTextMessage(CharBuffer arg0) throws IOException {		// TODO Auto-generated method stub			try {			//向特定的用户发送数据			WebSocketMessageInboundPool  inbound = Connections.get(user_nickname);			if(inbound != null){			CharBuffer cb = CharBuffer.wrap(arg0);			WsOutbound wb = inbound.getWsOutbound();			wb.writeTextMessage(cb);			wb.flush();			}		}		catch (IOException e) {				e.printStackTrace();			}		}		protected void onClose(int status) {		// TODO Auto-generated method stub		removeMessageInbound(this);		super.onClose(status);	}		//向连接池中添加连接	public static void addMessageInbound(WebSocketMessageInboundPool  inbound){		//添加连接		Connections.put(inbound.user_nickname, inbound);	}	public static void removeMessageInbound(WebSocketMessageInboundPool  inbound){		//移除连接		Connections.remove(inbound.user_nickname);	}}	
Copy after login




My handshake link is ws = new WebSocket("ws://localhost:8080/websocket/web"); / / Local
xml configuration:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 	xmlns="http://java.sun.com/xml/ns/javaee" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>	    <servlet>  	<servlet-name>WebSocketMessageServlet</servlet-name>  	<servlet-class>websocket.WebSocketMessageServlet</servlet-class>  </servlet>  <servlet-mapping>  	<servlet-name>WebSocketMessageServlet</servlet-name>  	<url-pattern>/web</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
Copy after login



But my local tomcat test still doesn’t work.
Do I need to delete all the jar packages in the lib folder of the tomcat project?



Reply to discussion (solution)

up............. .............

Does anyone know how?

public class WebSocketMessageServlet extends WebSocketServlet{     protected StreamInbound createWebSocketInbound(String arg0) {               System.out.println("$$$$$$$$");        return new WebSocketMessageInboundPool("1");      }
Copy after login


You can shake hands if you write it this way. It’s so strange. It is correct to overload the abstract method of the parent class.

I checked F12 on the browser and the error said this:

There is a problem with your xml configuration!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template