这是我的 Java 服务器代码。仅从客户端接收消息并回显该消息。我正在使用 WebSocket。
package pack.exp;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/serverendpointdemo")
public class EchoServer
{
@OnOpen
public void handleOpen()
{
System.out.println("Client is now Connected...");
}
@OnMessage
public String handleMessage(String Message)
{
System.out.println("Message receive From Client... " + Message);
String replyMsg= "Echo from Server..." + Message;
System.out.println("Message sent to Client... " + Message);
return replyMsg;
}
@OnClose
public void handleClose()
{
System.out.println("Client is now Disconnected...");
}
@OnError
public void handleError(Throwable t)
{
t.printStackTrace();
}
}
The client is in Java Script, The client is sending the message and receiving the echo
from the server.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form>
<input id="textMessage" type= "text">
<input onclick= "sendMessage"; value= "Send Message" type="button">
</form>
<br>
<textarea id= "messageTextArea" rows="80" cols="50"></textarea>
<script type="text/javascript">
var webSoc= new
WebSocket("ws//localhost:8080/WSocEcho/serverendpointdemo");
var messageTextArea= document.getElementById("messageTextArea");
webSoc.onopen= function(message)
{
processOpen(message;)
};
webSoc.onmessage= function(message)
{
processMessage(message;)
};
webSoc.onclose= function(message)
{
processClose(message);
}
webSoc.onerror(Message)
{
processError(message);
}
function processOpen(message)
{
messageTextArea.value= "Server Connect..." + "\n";
}
function processMessage(message)
{
messageTextArea.value= "Recieve from Server..." + message.data +
"\n";
}
function processClose(message)
{
webSoc.send("Client Disconnected...");
messageTextArea.value+= "Server Disconnect..." + "\n";
}
function processOpen(message)
{
messageTextArea.value+= "Error..." + "\n";
}
function sendMessage()
{
webSoc.send(textMessage.value);
messageTextArea.value+= "Sent to Server... " + textMessage.value +
"\n";
textMessage.value("");
}
</script>
</body>
</html>
我正在使用 Glass Fish 4.0 和 Eclipse Juno。但是当我跑步开始编码服务器,启动大约需要 7-8 分钟,Glass Fish
之后也会出现此错误
无法按时启动服务器
java.util.concurrent.TimeoutException