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配置有问题!