HTML5:WebSocket实现-创新互联

搞定这个的过程远比想象的困难许多。网上许多教程,一一尝试,大多随着库版本的改变以及浏览器对协议支持的改变以失败告终,即使成功的也语焉不详,以下为步骤。

成都创新互联-专业网站定制、快速模板网站建设、高性价比广汉网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式广汉网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖广汉地区。费用合理售后完善,十余年实体公司更值得信赖。

1. 运行环境

Java EE IDE

apache-tomcat-7.0.29(7.0.27之后才支持websocket)

IE(版本:10.0.9200.16384)

Firefox(版本:15.0)

Google Chrome(版本:23.0.1251.2)

2. 详细步骤

(1) 在Java EE IDE中,File->New->Dynamic Web Project,使其如下配置

Target runtime中必须是Apache Tomcat v7.0,并且最好是引用7.0.29(可在New Runtime中配置)

HTML5:WebSocket实现

注意,点击Next->,再点击Next->,如下图,注意勾选Generate web.xml deployment descriptor,点击Finish

HTML5:WebSocket实现

(2) 在Java Resources下src下New->Class,如下图

HTML5:WebSocket实现

再在src下New->Class,如下图

HTML5:WebSocket实现

下面贴上上述类的代码

package com.trump.web.servlet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
public class EchoServlet extends WebSocketServlet {
private static final long serialVersionUID = -4104682919545889813L;
private Logger logger = Logger.getLogger(EchoServlet.class.getName());
private static List socketList = new ArrayList();
@Override
protected StreamInbound createWebSocketInbound(String subProtocol,
HttpServletRequest request) {
// TODO Auto-generated method stub
logger.info("receive ws request");
return new MyMessageInbound();
}
public static synchronized List getSocketList() {
return socketList;
}
}
package com.trump.web.servlet;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.logging.Logger;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;
public class MyMessageInbound extends MessageInbound {
private int userIdName = 0;
private Logger logger = Logger.getLogger(MyMessageInbound.class.getName());
public int getUserIdName() {
return userIdName;
}
protected void onOpen(WsOutbound outbound) {
super.onOpen(outbound);
userIdName = outbound.hashCode();
EchoServlet.getSocketList().add(this);
logger.info("Server onOpen");
}
protected void onClose(int status) {
EchoServlet.getSocketList().remove(this);
super.onClose(status);
logger.info("Server onClose");
}
// 有二进制消息数据到达,比如音频等文件传输
@Override
protected void onBinaryMessage(ByteBuffer buffer) throws IOException {
logger.info("Binary Message Receive: " + buffer.remaining());
}
@Override
protected void onTextMessage(CharBuffer buffer) throws IOException {
String msgOriginal = buffer.toString();
int startIndex = msgOriginal.indexOf("!@#$%");
String nikeName = msgOriginal.substring(0, startIndex);
String textMsg = msgOriginal.substring(startIndex + 5);
// 将字符数组包装到缓冲区中
// 给定的字符数组将支持新缓冲区;即缓冲区修改将导致数组修改,反之亦然
String countMsg = EchoServlet.getSocketList().size() + "人同时在线";
logger.info("Server onTextMessage: " + countMsg + nikeName + ":"
+ textMsg);
String msg1 = nikeName + ": " + textMsg;
String msg2 = "我: " + textMsg;
for (MyMessageInbound messageInbound : EchoServlet.getSocketList()) {
CharBuffer msgBuffer1 = CharBuffer.wrap(msg1);
CharBuffer msgBuffer2 = CharBuffer.wrap(msg2);
WsOutbound outbound = messageInbound.getWsOutbound();
if (messageInbound.getUserIdName() != this.getUserIdName()) {
outbound.writeTextMessage(msgBuffer1);
outbound.flush();
} else {
outbound.writeTextMessage(msgBuffer2);
}
}
}
}
(3) 打开WebContent下WEB-INF下web.xml,编辑为


myWebSocket

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp


echo
com.trump.web.servlet.EchoServlet


echo
*.ws

(4) 右击项目myWebSocket->Run As->Run on Server,如下图,点击Finish,

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:HTML5:WebSocket实现-创新互联
文章分享:http://cdiso.cn/article/psodh.html

其他资讯