pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
JavaNative
JavaNative
1.0-SNAPSHOT
jar
JavaNative
http://maven.apache.org
UTF-8
org.glassfish.tyrus.bundles
tyrus-standalone-client
1.9
websocket.client.SimpleClientpackage websocket.client;
import javax.websocket.*;
import java.io.IOException;
import java.net.URI;
/**
* Created by Administrator on 2016/4/17.
*/
@ClientEndpoint
public class SimpleClient {
@OnOpen
public void onOpen(Session session) {
System.out.println("open ... ");
}
@OnMessage
public void onMessage(String message) {
System.out.println(message);
}
@OnError
public void onError(Throwable t) {
t.printStackTrace();
}
public static void main(String[] args)throws Exception{
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri ="ws://localhost:8077/web/SimpleWebSocket";
System.out.println("Connecting to 2"+ uri);
Session session = container.connectToServer(SimpleClient.class, URI.create(uri));
session.getBasicRemote().sendText("这是从java客户端发送的消息 。。。");
Thread.sleep(1000);
session.close();
}
}