PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

【websocet】这样写能握手成功吗?_html/css_WEB-ITnose

原创
2016-06-24 12:02:29 841浏览

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); 	}	 }  

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 Connections = new HashMap();		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);	}}	




我的握手链接是ws = new WebSocket("ws://localhost:8080/websocket/web"); // 本地
xml配置:
  	      	WebSocketMessageServlet  	websocket.WebSocketMessageServlet      	WebSocketMessageServlet  	/web          index.jsp  



但是我本地tomcat测试了还是不行,
tomcat项目lib文件夹下的jar包全都要删掉吗?



回复讨论(解决方案)

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

木有人会吗?

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


这样写就能握手,太奇怪了,明明要重载父类抽象方法才是正确的。

我在浏览器上F12看了一下,错误是这样说的

楼主你的xml配置有问题!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。