websocket

qq_38594151 2017-12-07 08:55:14
java代码
import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint("/echo")
public class EchoServer {
@OnMessage
public String echo(String incomingMessage){
return "I got this{"+incomingMessage+"}"
+"so i am sending it back !";
}

}

html代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content_Type" content="text/html"
charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" type="text">
var echo_websocket;
function init(){
output = document.getElementById("output");
}
function send_echo(){
var wsUri = "ws://localhost:8080/echoserver/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.date);
echo_websocket.close();
};
echo_websocket.onerror = function (evt){
writeToScreen('<spen style = "color : red;">
ERROR:</span>' + evt.date);


echo_websocket.close();
};

}
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 onclick = "textID" name = "message" value = "Hello world" type = "text">
<br>
</form>
</div>
<div id="output"></div>
</body>
</html>

2个错误
第一个是直接报错
第二个是 send_echo 未定义
...全文
151 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_38594151 2017-12-07
  • 打赏
  • 举报
回复
求大神帮帮忙
qq_38594151 2017-12-07
  • 打赏
  • 举报
回复
引用 2 楼 powg86 的回复:
我叫施新欣 2017-12-07
  • 打赏
  • 举报
回复

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧