我正在尝试运行websocket示例。
我的客户端代码就是这个
<!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>Web Socket Implementation</title>
</head>
<body>
<form>
<input id="textmessage" type="text">
<input onclick="sendmessage();" value="Send Message" type="button">
</form>
<br><textarea id="messageTextArea" rows="10" cols="50"></textarea>
<script type="text/javascript">
var webSocket=new
WebSocket("ws://localhost:8080/SimpleWebSocketProj1/serverendpoindemo");
var messageTextArea=document.getElementById("messageTextArea");
webSocket.onopen=function(message){processOpen(message);};
webSocket.onmessage=function(message){processMessage(message);};
webSocket.onclose=function(message){processClose(message);};
webSocket.onerror=function(message){processError(message);};
function processOpen(message)
{
messageTextArea.value +="Server Connect.." + "\n";
}
function onmessage(message)
{
messageTextArea.value +="Receive from server..-->" + message.data
+"\n";
}
function sendmessage()
{ if(textmessage.vaue!="close")
{
webSocket.send(tesxtmessage.value);
messageTextArea.value +="Send to server..-->" +
textmessage.value +"\n";
tesxtmessage.value="";
}
else
webSocket.close();
}
function processClose(message)
{
webSocket.send("Client disconnceted..");
messageTextArea.value +="Server Disconnect.." + "\n";
}
function processError(message)
{
messageTextArea.value +="Error while Connect..\n";
}
</script>
</body>
</html>
这是我的服务器java代码
package com.ws.test;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/serverendpoindemo")
public class ServerEndpoinDemo
{ @OnOpen
public void handleOpen()
{
System.out.println("client is now connected..");
}
@OnMessage
public String handleMessage(String message)
{
System.out.println("receive from client.." +message);
String replymessage="echo " +message;
System.out.println("send to client.." +replymessage);
return replymessage;
}
@OnClose
public void handleClose()
{
System.out.println("client is now disconnected..");
}
@OnError
public void handleError(Throwable t)
{
t.printStackTrace();
}
}
当我尝试在服务器上运行时,它会显示如下错误 Eclipse插件无法与GlassFish服务器通信,状态为:CREDENTIAL_ERROR
答案 0 :(得分:0)
也许安装glassfish有问题,请确保你的glassfish中有正确的文件路径和eclipse中正确的JDK。 当您创建一个新的动态Web项目时,请转到:target runtime&gt; glassfish 4并传递glassfish服务器目录:C:.. \ glassfish4 \ glassfish 然后在同一个屏幕中添加JDK而不是JRE,文件位置为:C:.. \ Java \ jdk1.7.0_51。如果您没有JDK下载它。点击完成,它应该工作。