我正在尝试使用 javax.websocket 创建聊天。我正在使用 grails。3.0
这是我的控制器
package chatting
import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;
@ServerEndpoint("/echo")
public class WebsocketHomeController {
def index() { }
@OnMessage
public String echo(String incomingMessage) {
return "I got this (" + incomingMessage + ")"
+ " so I am sending it back !";
}
}
这是我的 index.gsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Web Socket JavaScript Echo Client</title>
<script language="javascript" type="text/javascript">
var echo_websocket;
function init() {
output = document.getElementById("output");
}
function send_echo() {
var wsUri = "ws://192.168.1.2:8080/WebsocketHome/echo";
writeToScreen("Connecting to " + wsUri);
echo_websocket = new WebSocket(wsUri);
echo_websocket.onopen = function (evt) {
writeToScreen("Connected !");
doSend(textID.value);
};
echo_websocket.onmessage = function (evt) {
writeToScreen("Received message: " + evt.data);
echo_websocket.close();
};
echo_websocket.onerror = function (evt) {
writeToScreen('<span style="color: red;">ERROR:</span> '
+ evt.data);
echo_websocket.close();
};
}
function showErrorInfo(e) {
alert('Error connecting socket'+e);
}
function doSend(message) {
echo_websocket.send(message);
writeToScreen("Sent message: " + message);
}
function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
</head>
<body>
<h1>Echo Server</h1>
<div style="text-align: left;">
<form action="">
<input onclick="send_echo()" value="Press to send"
type="button">
<input id="textID" name="message" value="Hello Web Sockets"
type="text">
<br>
</form>
</div>
<div id="output"></div>
</body>
</html>
这是结果..
我不知道..为什么我会收到这个错误...
我试图像这样调试我的代码..
public String echo(String incomingMessage) {
println "this is runing"
return "I got this (" + incomingMessage + ")"
+ " so I am sending it back !";
}
然后我在控制台日志中得到“这正在运行”
我必须做什么?