看了网上websocket的例子,为什么一直调不通呢.
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://109.123.112.65:8080/TestWeb/Test');
else if ('MozWebSocket' in window)
ws = new MozWebSocket("ws://109.123.112.65:8080/TestWeb/Test");
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>
Test.java源码import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;import javax.servlet.http.HttpServletRequest;import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;/**
* Servlet implementation class Test
*/
public class Test extends WebSocketServlet { private static final long serialVersionUID = 1L;
private Log log = LogFactory.getLog(getClass()); @Override
protected StreamInbound createWebSocketInbound(String arg0, HttpServletRequest arg1) {
log.info("======================");
return new MessageInbound() {
@Override
protected void onTextMessage(CharBuffer charBuffer) throws IOException {
log.info("get message from client:"+charBuffer);
WsOutbound out=this.getWsOutbound();
CharBuffer buffer=CharBuffer.wrap("I'am server,I recevied yourmessage:"+charBuffer);
out.writeTextMessage(buffer);
out.flush();
}
@Override
protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
// TODO Auto-generated method stub
}
};
}
}为什么运行没有反应呢,直接alert("close");
求解答
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://109.123.112.65:8080/TestWeb/Test');
else if ('MozWebSocket' in window)
ws = new MozWebSocket("ws://109.123.112.65:8080/TestWeb/Test");
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>
Test.java源码import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;import javax.servlet.http.HttpServletRequest;import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;/**
* Servlet implementation class Test
*/
public class Test extends WebSocketServlet { private static final long serialVersionUID = 1L;
private Log log = LogFactory.getLog(getClass()); @Override
protected StreamInbound createWebSocketInbound(String arg0, HttpServletRequest arg1) {
log.info("======================");
return new MessageInbound() {
@Override
protected void onTextMessage(CharBuffer charBuffer) throws IOException {
log.info("get message from client:"+charBuffer);
WsOutbound out=this.getWsOutbound();
CharBuffer buffer=CharBuffer.wrap("I'am server,I recevied yourmessage:"+charBuffer);
out.writeTextMessage(buffer);
out.flush();
}
@Override
protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
// TODO Auto-generated method stub
}
};
}
}为什么运行没有反应呢,直接alert("close");
求解答
解决方案 »
- 用eclispe连接svn资源库出现了一个这样的错误,那位大虾,帮我解决一下
- log4j或logback 打印日志到不同文件
- JfreeChart中热点map的链接 乱码问题
- 大家帮忙看一看!!
- 帮忙修正下,里面的Exception怎么写
- java.lang.ExceptionInInitializerError错误,那位大虾看看我贴子里的内容,帮忙解决,我实在是没有方法了.
- 【JAVA实习生求助】
- ###麻烦大家帮我做份试卷###
- 请问高手,垃圾回收器能不能回收Class.forName()动态加载类的内存?
- spring整合配置事物报错
- 正则表达式
- 关于在java中调用C++动态库的问题,多次调用出现冲突,怎么才能解决呢?
刚上传的例子 LZ请趁热