FireFox 说我的方法 doSend 没有定义,但它是。
Chrome 对 websocket 只字未提……
这是我的代码:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<script type="text/javascript">
var image;
var output;
function init() {
console.log('CONNECTED to websockets!');
output = document.getElementById('output');
testWebSocket();
}
//connects websocket to KwetterWebsocketBean
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
onOpen(evt);
};
websocket.onclose = function(evt) {
onClose(evt);
};
websocket.onmessage = function(evt) {
onMessage(evt);
};
websocket.onerror = function(evt) {
onError(evt);
};
}
//define event handlers
function onOpen(evt) {
}
function onWindowClose(evt){
websocket.close();
}
function onClose(evt) {
}
function onMessage(evt) {
//convert json to javascript object
var message = JSON.parse(evt.data);
writeToScreen('<span style="color: green;">New tweet by ' + message.username + ': ' + message.text + '</span>');
console.log(message.text + ' : ' + message.username);
//write message.text to screen
}
function onError(event) {
}
function doSend(message) {
console.log(message);
websocket.send(message);
}
//appends text to #output
function writeToScreen(text) {
var pre = document.createElement('p');
pre.style.wordWrap = 'break-word';
pre.innerHTML = text;
output.appendChild(pre);
}
//invoke init() on load
window.addEventListener('load', init, false);
//enter key clicks #sendButton
function keyPressed(event){
if(event.keyCode == 13){
document.getElementById('sendButton').click();
document.getElementById('textforws').value='';
}
}
</script>
</h:head>
<h:body>
<ui:composition template="./aTemplate.xhtml">
<ui:define name="S1">
<h1>What's happening?</h1>
<h:form id="formNewestTweet">
<p:inputText maxlength="140" value="aMessage"/>
<p:commandButton value="Post Tweet" id="sendButton"
onclick='doSend("aMessage");'/>
</h:form>
<div id="output"/>
</ui:define>
</ui:composition>
</h:body>
</html>
我应该如何解决这个问题?