现在tomcat7.0.5x支持websocket的哪个协议版本

 我来答
huanglenzhi
推荐于2016-05-27 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
由于安卓4.0不支持websocket,我在网上看见解决方案(使用在android的webview里用js调用java,实现websocket,见实例),
症状:connect直接打印 closed
但是发现,websocket.java中使用的websocket版本为draft75和76版本。服务器使用的tomcat7.0.50,请大家帮我分析下,是不是因为new webscocket(url, draft75),使得tomcat不支持老版本的草案。
代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style type="text/css">
#connect-container {
float: left;
width: 400px
}

#connect-container div {
padding: 5px;
}

#console-container {
float: left;
margin-left: 15px;
width: 400px;
}

#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}

#console p {
padding: 0;
margin: 0;
}
</style>
<script src="js/websocket.js"></script>
<script type="text/javascript">
var ws = null;

function setConnected(connected) {
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('echo').disabled = !connected;
}

function connect() {
var target = document.getElementById('target').value;
if (target == '') {
alert('Please select server side connection implementation.');
return;
}
// if ('WebSocket' in window) {
// ws = new WebSocket(target);
// } else if ('MozWebSocket' in window) {
// ws = new MozWebSocket(target);
// } else {
// alert('WebSocket is not supported by this browser.');
// return;
// }
ws = new WebSocket(target);
ws.onopen = function () {
setConnected(true);
log('Info: WebSocket connection opened.');
};
ws.onmessage = function (event) {
log('Received: ' + event.data);
};
ws.onclose = function () {
setConnected(false);
log('Info: WebSocket connection closed.');
};
}

function disconnect() {
if (ws != null) {
ws.close();
ws = null;
}
setConnected(false);
}

function echo() {
if (ws != null) {
var message = document.getElementById('message').value;
log('Sent: ' + message);
ws.send(message);
} else {
alert('WebSocket connection not established, please connect.');
}
}

function updateTarget(target) {
if (window.location.protocol == 'http:') {
document.getElementById('target').value = 'ws://' + window.location.host + target;
} else {
document.getElementById('target').value = 'wss://' + window.location.host + target;
}
}

function log(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop = console.scrollHeight;
}
</script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></noscript>
<div>
<div id="connect-container">
<span>Test</span>
<div>
<span>Connect using:</span>
<!-- echo example using streams on the server side -->
<input id="radio1" type="radio" name="group1" value="/examples/websocket/tc7/echoStream"
onclick="updateTarget(this.value);"> <label for="radio1">streams</label>
<!-- echo example using messages on the server side -->
<input id="radio2" type="radio" name="group1" value="/websocket/websocket/tc7/adeviceMessage"
onclick="updateTarget(this.value);"> <label for="radio2">messages</label>
</div>
<div>
<input id="target" type="text" size="40" style="width: 350px"/>
</div>
<div>
<button id="connect" onclick="connect();">Connect</button>
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
</div>
<div>
<textarea id="message" style="width: 350px">Here is a message!</textarea>
</div>
<div>
<button id="echo" onclick="echo();" disabled="disabled">Echo message</button>
</div>
</div>
<div id="console-container">
<div id="console"></div>
</div>
</div>
</body>
</html>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式