我完成了聊天应用程序的服务器编码。我能够将响应发送到服务器并检索它。现在我正在进行客户端编码。
这是我的第一个文件
public class Client
{
public static void main(String ar[]) throws URISyntaxException, DeploymentException, IOException
{
ChatRoomClientEndPoint chatroomClientEndPoint = new ChatRoomClientEndPoint();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter your username");
String message = null;
while(true)
{
message = bufferedReader.readLine();
chatroomClientEndPoint.sendMessage(message);
}
}
}
这是我的第二个文件
@ClientEndpoint
public class ChatRoomClientEndPoint
{
Session session = null;
public ChatRoomClientEndPoint() throws URISyntaxException, DeploymentException, IOException
{
URI uRI = new URI("ws://localhost:8080/WebSocketPrj01/ServerEndPointDemo");
ContainerProvider.getWebSocketContainer().connectToServer(this,uRI);
}
@OnOpen
public void processOpen(Session session)
{
this.session = session;
}
@OnMessage
public void processMessage(String message)
{
System.out.println(Json.createReader( new StringReader(message)).readObject().getString("message"));
}
public void sendMessage(String message) throws IOException
{
session.getBasicRemote().sendText(message);
}
}
我正在使用netbeans,而ServerEndPointDemo是在netbeans中名为WebSocketPrj01的另一个项目中。当我运行Client类时,它会给出以下异常
线程中的异常" main" javax.websocket.DeploymentException:握手错误。